Swift ios手机应用登录提交



我正在尝试为我们学校的这个网站编写一个iOS应用程序:https://uniworx.ifi.lmu.de/

登录失败。我已经管理它发送一个http提交的登录表单,但我总是得到相同的网站回来作为响应。我想在登录后获得您通常看到的网站。我做错了什么?

下面是我的函数代码:
 func newTest()
{
    let request = NSMutableURLRequest(url: NSURL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")! as URL)
    request.httpMethod = "POST"
    let postString = "username=myusername&password=mypassword"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        guard error == nil && data != nil else {                                                          
            print("error=(error)")
            return
        }
        if let httpStatus = response as? HTTPURLResponse , httpStatus.statusCode != 200 {          
            print("statusCode should be 200, but is (httpStatus.statusCode)")
            print("response = (response)")
        }
        let responseString = String(data: data!, encoding: String.Encoding.utf8)
        print("responseString = (responseString)")

    }
    task.resume()


}

我已经在代码中更改了我的真实用户名和密码-当然-

试试这个,希望对你有帮助!!

在Swift 2+

let request = NSMutableURLRequest(URL: NSURL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")!)
    request.HTTPMethod = "POST"
    let postString = "Username=Yourusername&password=Yourpassword"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        guard error == nil && data != nil else {                                                          // check for fundamental networking error
            print("error=(error)")
            return
        }
        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is (httpStatus.statusCode)")
            print("response = (response)")
        }
        let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = (responseString)")
    }
    task.resume()

在Swift 3中可以

var request = URLRequest(url: URL(string: "http://uniworx.ifi.lmu.de/?action=uniworxLoginDo")!)
request.httpMethod = "POST"
let postString = "Username=Yourusername&password=Yourpassword"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error == nil else {                                                 // check for fundamental networking error
        print("error=(error)")
        return
    }
    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
        print("statusCode should be 200, but is (httpStatus.statusCode)")
        print("response = (response)")
    }
    let responseString = String(data: data, encoding: .utf8)
    print("responseString = (responseString)")
}
task.resume()

最新更新