我张贴NSURLMutableRequest
同时使用Alamofire和类似的自定义方法的WebAPI:差异的NSURLRequest
Alamofire
Alamofire.request(.POST, "http://www.webapi.com?path=Login", parameters: ["username": "myname", "password": "mypass"]).responseJSON { response in
// do something with returned data ...
}
定制
let request = NSMutableURLRequest(URL: NSURL(string:"http://www.webapi.com?path=Login")!)
let bodyData = "username=myname&password=mypass"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
// do something with returned data...
}.resume()
我通过Alamofire方法得到了正确的结果,但不是我自己的 方法。
与Alamofire post方法相比,我做了什么错误?
可以打开Alamofire.request方法。 alamofire的代码是开源的。 – Sahil
当你说你得到了正确的结果; NSURLRequest的错误结果是什么?这是一个崩溃,从任务返回的错误,错误的数据? – keithbhunter
@keithbhunter正确的结果是json格式化的数据和不正确的结果只是'无' – bluenowhere