Jab*_*bar 10 web-services http http-headers ios swift
我正在按照这个答案在我的swift项目中进行HTTP调用.如何在Swift中发出HTTP请求?
以下是我用来进行同步调用的代码
let urlPath: String = "http://apiserver.com/api/login/?username=asdf&password=asdf"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
var error: NSErrorPointer = nil
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
var err: NSError
println("response -- \(response)")
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
println("Synchronous \(jsonResult)")
Run Code Online (Sandbox Code Playgroud)
在这里有谁可以帮助我使用此代码获取HTTP标头响应或状态代码?请
iAn*_*rag 13
试试这个:
func getData(url: NSURL) {
let config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session: NSURLSession = NSURLSession(configuration: config)
let dataTask: NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: {(data: NSData!, urlResponse: NSURLResponse!, error: NSError!) -> Void in
if let httpUrlResponse = urlResponse as? NSHTTPURLResponse
{
if error {
println("Error Occurred: \(error.localizedDescription)")
} else {
println("\(httpUrlResponse.allHeaderFields)") // Error
}
}
})
dataTask.resume()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10427 次 |
| 最近记录: |