gpt4 book ai didi

HTTP 请求适用于 Postman 但不适用于 Swift

转载 作者:行者123 更新时间:2023-11-28 13:20:33 25 4
gpt4 key购买 nike

我正在开发一个简单的应用程序,它必须登录到我们大学的网站并将我卡上的 saldo 还给我。

当我用 postman 执行这个简单的请求时,它工作正常,我得到了登录的 HTML 页面。在 XCODE 上执行相同的请求不起作用:

override func viewDidLoad() {
super.viewDidLoad()


var dataString = "j_username=USERNAME&j_password=PASSWORD"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: "https://campuscard.hhs.nl/portal/j_spring_security_check")
var postString = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
//request.setValue("JSESSIONID=C78C688403A836968EC1FEAED9AE9126", forHTTPHeaderField: "Cookie")
//request.setValue("campuscard.hhs.nl", forHTTPHeaderField: "Host");
request.setValue("keep-alive", forHTTPHeaderField: "Connection");
request.setValue("41", forHTTPHeaderField: "Content-Length");
//request.setValue("max-age=0", forHTTPHeaderField: "Cache-Controle");
//request.setValue("*/*", forHTTPHeaderField: "Accept");
//request.setValue("https://campuscard.hhs.nl", forHTTPHeaderField: "Origin");
//request.setValue("https://campuscard.hhs.nl/portal/login", forHTTPHeaderField: "Referer");
//request.setValue("gzip,deflate", forHTTPHeaderField: "Accept-Encoding");
//request.setValue("nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4", forHTTPHeaderField: "Accept-Language");
request.HTTPBody = postString
println(request);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
}

页面无法登录...打印的请求如下所示:

<NSMutableURLRequest: 0x7fecdb54b720> { URL: https://campuscard.hhs.nl/portal/j_spring_security_check, headers: {
Accept = "*/*";
"Accept-Encoding" = "gzip,deflate";
"Accept-Language" = "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4";
"Cache-Controle" = "max-age=0";
Connection = "keep-alive";
"Content-Length" = 41;
"Content-Type" = "application/x-www-form-urlencoded";
} }

有人知道 postman 发送的请求和我执行的请求有什么区别吗?

提前致谢!

最佳答案

在这种特殊情况下,还必须在请求中发送安全 token 。我通过在 Google Chrome 调试器中密切监视请求解决了这个问题。

关于HTTP 请求适用于 Postman 但不适用于 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26326900/

25 4 0
文章推荐: JQuery UI 自动完成实现不工作
文章推荐: css - IIS - 浏览文件时拒绝访问
文章推荐: javascript - 递归遍历树以创建面包屑列表
文章推荐: javascript - 将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com