gpt4 book ai didi

ios - swift ios 移动应用程序登录提交

转载 作者:行者123 更新时间:2023-11-29 00:38:20 24 4
gpt4 key购买 nike

我正在尝试为我们大学的这个网站编写一个 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()

关于ios - swift ios 移动应用程序登录提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108462/

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