这是我的两个例子:
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": UIDevice.currentDevice().model]
var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX"))
request.HTTPMethod = "POST"
let valuesToSend = ["key":value, "key2":value]
var error: NSError?
let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error)
request.HTTPBody = data
if error == nil {
let task = NSURLSession(configuration: config).dataTaskWithRequest(request,
completionHandler: {data, response, error in
if error == nil {
println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))")
}
})
task.resume()
} else {
println("Oups error \(error)")
}
和第二个
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": UIDevice.currentDevice().model]
var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX"))
request.HTTPMethod = "POST"
let valuesToSend = ["key":value, "key2":value]
var error: NSError?
let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error)
if error == nil {
let task = NSURLSession(configuration: config).uploadTaskWithRequest(request, fromData: data,
completionHandler: {data, response, error in
if error == nil {
println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))")
}
})
task.resume()
} else {
println("Oups error \(error)")
}
所以我想知道:这两个例子之间有什么区别,以及对我的情况更好的情况(简单的邮寄和接待)
两人是背景不是吗?所以 ?