gpt4 book ai didi

ios - swift 中的 URL 编码

转载 作者:行者123 更新时间:2023-11-28 07:51:42 24 4
gpt4 key购买 nike

请帮我在 Swift 3 上做同样的事情

curl -X POST -d MD='44586989' -d TermUrl='https://proil.moscow/' --data-urlencode 'PaReq=+/eyJNZXJjaGFudE5hbWUiOm51bGwsIkZpcnN0U2l4IjoiNDI3NjM4IiwiTGFzdEZvdXIiOiIxMTQ1IiwiQW1vdW50IjoxMDM0LjAwLCJDdXJyZW5jeUNvZGUiOiJSVUIiLCJEYXRlIjoiMjAxOC0wMy0yNlQwMDowMDowMCswMzowMCIsIkN1c3RvbWVyTmFtZSI6bnVsbCwiQ3VsdHVyZU5hbWUiOiJydS1SVSJ9' https://demo.cloudpayments.ru/acs

我试试这个:

 var request = URLRequest(url: URL(string: acsUrlString)!)
request.httpMethod = "POST"
let session = URLSession.shared


request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let d = String("PaReq=" + pareq + "&MD=" + String(Int(transactionId)) + "&TermUrl=https://proil.moscow").data(using:String.Encoding.ascii, allowLossyConversion: false)
request.httpBody = d

where d without encoding = PaReq=+/eyJNZXJjaGFudE5hbWUiOm51bGwsIkZpcnN0U2l4IjoiNDI3NjM4IiwiTGFzdEZvdXIiOiIxMTQ1IiwiQW1vdW50IjoxMDM0LjAwLCJDdXJyZW5jeUNvZGUiOiJSVUIiLCJEYXRlIjoiMjAxOC0wMy0yNlQwMDowMDowMCswMzowMCIsIkN1c3RvbWVyTmFtZSI6bnVsbCwiQ3VsdHVyZU5hbWUiOiJydS1SVSJ9&MD=44586989&TermUrl=https://proil.moscow

所以这段代码给了我相同的结果

curl -X POST -d MD='44586989' -d TermUrl='https://proil.moscow/' -d PaReq='+/eyJNZXJjaGFudE5hbWUiOm51bGwsIkZpcnN0U2l4IjoiNDI3NjM4IiwiTGFzdEZvdXIiOiIxMTQ1IiwiQW1vdW50IjoxMDM0LjAwLCJDdXJyZW5jeUNvZGUiOiJSVUIiLCJEYXRlIjoiMjAxOC0wMy0yNlQwMDowMDowMCswMzowMCIsIkN1c3RvbWVyTmFtZSI6bnVsbCwiQ3VsdHVyZU5hbWUiOiJydS1SVSJ9' https://demo.cloudpayments.ru/acs

我从服务器 cloudpayments 得到这个响应:“检查 PaReq 参数中的‘+’符号。”,

最佳答案

谢谢你,Rob,你的回答对我有帮助。

request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = postParameters.map { key, value in
let keyString = key.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)!
let valueString = (value as! String).addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)!
return keyString + "=" + valueString
}.joined(separator: "&").data(using: .utf8)

和扩展

extension CharacterSet {

static var urlQueryValueAllowed: CharacterSet = {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="

var allowed = CharacterSet.urlQueryAllowed
allowed.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)

return allowed
}()
}

关于ios - swift 中的 URL 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496121/

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