gpt4 book ai didi

json - 错误: when post on Swift

转载 作者:行者123 更新时间:2023-11-30 10:08:36 24 4
gpt4 key购买 nike

我从后端开发人员那里获得了以下后期服务,现在我正在尝试在 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()

}

当我嗅探网站时,下面的响应和请求 header ;

响应 header

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

请求 header

 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

最佳答案

在我使用下面的 jsonString 和 request.HTTPBody 方法更改字典后,我的代码开始工作。

let form1 = "product_id=297&order_no=010215135311&city=Paris&…”

request.HTTPBody = form1.dataUsingEncoding(NSUTF8StringEncoding)

我认为这是编码问题,但我不确定。

关于json - 错误: when post on Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com