我正在开发一个简单的应用程序,它需要登录我们大学的网站,然后把我卡上的萨尔多动作还给我。
当我对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 发送的请求和我执行的请求之间的区别吗?
先谢了!
1条答案
按热度按时间pgky5nke1#
在这种特殊情况下,还必须在请求中发送一个安全令牌,我通过在Google Chrome调试器中密切监视请求来解决这个问题。