gpt4 book ai didi

ios - 如何在WKWebView中使用参数加载POST URLRequest?

转载 作者:行者123 更新时间:2023-12-01 21:59:19 25 4
gpt4 key购买 nike

对不起,我的英语不好
我尝试了多种类型的解决方案,但无法在Xcode 11.2.1swift 5中使用

我尝试这个

var urlRequest = URLRequest(url: URL(string: "https://xxxxxx/login")!)  
urlRequest.httpMethod = "POST"
let params = [
"username": SessionManager.shared.username!,
"password": SessionManager.shared.password!,
"vhost": "standard"
]
let postString = self.getPostString(params: params)
urlRequest.httpBody = postString.data(using: .utf8)
webView.load(urlRequest)


...
//helper method to build url form request
func getPostString(params:[String:String]) -> String
{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")

}
return data.map { String($0) }.joined(separator: "&")
}

和这个

Post Request with Parameter

并尝试在我的代码中添加以下行
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

但不起作用
我触发请求,是因为 WKWebView屏幕无法正常工作,但没有打开请求。
如果我没有设置navigationDelegate并打开普通的 URL,那么它可以完全正常工作
如果我设置了navigationDelegate,那么所有页面都将出现空白请求,例如Normal URL火或post参数 URL火,所有页面都将进入空白页面
我不明白 WKWebView有什么问题
请帮我。
提前致谢

最佳答案

请求正文使用与查询字符串相同的格式:

parameter=value&also=another

因此,您请求的内容类型为application / x-www-form-urlencoded类型:
let postString = self.getPostString(params: params)  

urlRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
urlRequest.httpMethod = "POST"
urlRequest.httpBody = postString.data(using: .utf8)

webView.load(urlRequest)

关于ios - 如何在WKWebView中使用参数加载POST URLRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60538973/

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