我還沒有能夠發送一個迅速的post-request到一個接受字符串的服務器。服務器不接受JSON數據。蘋果庫中是否有任何方法?我找不到任何。用字符串發送後請求
或者你甚至可以告訴我怎麼寫這樣的方法?
let url = NSURL(string: "myUrl")
var session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
var params = "login_act:'myUsername'&login_pwd:'myPassword'"
var err: NSError?
request.HTTPBody = NSJSONReadingOptions.
// The following method call does not accept the params variable type, although I need to send a String
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
任何幫助表示讚賞!
我需要使用你的第一個建議。但這些參數不包含在您的請求中。我怎麼做? – 2014-10-05 20:33:50
請參閱我的編輯。 – Sandeep 2014-10-06 08:35:06