0

我正在开发一个简单的应用程序,该应用程序必须登录我们大学的网站并将我卡上的萨尔多归还给我。

当我用邮递员执行这个简单的请求时,它工作正常,我得到了登录的 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";
} }

有人知道邮递员发送的请求和我执行的请求之间有什么区别吗?

提前致谢!

4

1 回答 1

2

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

于 2015-03-02T09:56:00.203 回答