我从后端开发人员那里获得了以下服务,现在我正试图在swift上实现它。
<form role="form" method="post" action=“order_post.asp" id="frmBack">
<input type="hidden" name="product_id" value="297">
<input type="hidden" name="order_no" value="010223005373">
<input type="hidden" name=“city” value=“Paris”>
<input type="hidden" name=“county” value=“La Fayette”>
<input type="hidden" name="price" value="143">
<input type="hidden" name=“receiver_name” value=“Jane Doe”>
<input type="hidden" name=“sender_name” value=“John Doe“>
<input type="submit" id="submit_back_handle">
我已经编写了下面的代码,但它没有用,并要求我填写表单上的所有字段。谁能告诉我我错过了什么?
func post() {
let params = [
“product_id": "297",
“order_no": "010215135311",
“city”: “Paris”,
“county”: “La Fayette“,
"price": "143",
"receiver_name": “Jane Doe“,
"sender_name": “John Doe”,
] as Dictionary <String, String>
let request = NSMutableURLRequest(URL: NSURL(string: "http://webservis.mydomain.com/order_post.asp")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
let success = json["success"] as? Int // Okay, the `json` is here, let's get the value for 'success' out of it
print("Success: \(success)")
} else {
let cfEnc = CFStringEncodings.ISOLatin5
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let jsonStr = NSString(data: data!, encoding: enc) // No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError) // Log the error thrown by `JSONObjectWithData`
let cfEnc = CFStringEncodings.ISOLatin5
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let jsonStr = NSString(data: data!, encoding: enc)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
}
当我嗅探网站时,下面会有回复和请求标题;
响应标头
Cache-Control:private Content-Encoding:gzip Content-Length:6876 Content-Type:text/html Date:Sat, 02 Jan 2016 16:11:30 GMT Server:Microsoft-IIS/8.5 Set-Cookie:xxx; secure; path=/ Vary:Accept-Encoding X-Powered-By:ASP.NET X-Powered-By-Plesk:PleskWin
请求标题
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:526 Content-Type:application/x-www-form-urlencoded Cookie:xxx Host:xxx Origin:xxx Referer:xxx Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) > AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
答案 0 :(得分:1)
我用以下jsonString和request.HTTPBody方法更改字典后,我的代码开始工作了jsonString。
let form1 = "product_id=297&order_no=010215135311&city=Paris&…”
request.HTTPBody = form1.dataUsingEncoding(NSUTF8StringEncoding)
我认为这是编码问题,但我不确定。