gpt4 book ai didi

ios - NSURLSession 认证

转载 作者:行者123 更新时间:2023-11-28 18:29:14 30 4
gpt4 key购买 nike

我尝试使用找到的 swift 代码 here在网站上找到here ,但响应是带有两个错误的 html 代码:“您必须输入密码!”和“您必须输入用户名!”我是 NSURLSession 的新手,并尝试更改用于身份验证的字符串,但没有任何方法可以更改响应。这是我的代码:

 let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "username@gmail.com:password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
var running = false
let url = NSURL(string: "https://hac.chicousd.org/")
let task = session.dataTaskWithURL(url!) {
(let data, let response, let error) in
if (response as? NSHTTPURLResponse) != nil {
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
}
running = false
}

running = true
task.resume()

while running {
print("waiting...")
sleep(1)
}

这是在控制台响应中:

<!-- Start Error box -->
<div id="errorContainer" class="alert alert-danger" style="display:none;">
<button id="portalCloseError" type="button" class="close"> &times;</button>
<span id="errorMessage" class="error" data-pw-error="You must enter a password!" data-username-error="You must enter a User Name!"></span>
</div> <!-- End Error box -->

最佳答案

登录网站有两种方法:HTTP 授权和表单。大多数网站使用较晚的、对用户更友好的登录表单。 HTTP 授权仅在网站期望与用户程序交互时使用(不,浏览器不算在这里)。

在编写代码之前,您需要搜索网站以了解它发送的数据格式,因为了解就成功了一半:

  1. 访问the school website然后按 Cmd + Opt + C 打开开发者控制台。
  2. 点击网络标签。
  3. 不要关闭这个控制台。返回网站并像往常一样输入您的用户名和密码。

观察页面发送了什么数据:

Developer Console

纯文本:

checkCookiesEnabled         true
checkMobileDevice false
checkStandaloneMode false
checkTabletDevice false
portalAccountUsername email
portalAccountPassword password
portalAccountUsernameLabel

因此,用户名在名为 portalAccountUsername 的字段中传递,密码在 portalAccountPassword 中传递。其他字段可能重要也可能不重要。

现在您可以编写代码了:

@IBAction func login(sender: AnyObject) {
let url = NSURL(string: "https://hac.chicousd.org/LoginParent.aspx")!

// The form parameters
let parameters = [
"checkCookiesEnabled": "true",
"checkMobileDevice": "false",
"checkStandaloneMode": "false",
"checkTabletDevice": "false",
"portalAccountUsername": "username",
"portalAccountPassword": "password"
]

let bodyString = parameters
.map { $0 + "=" + $1 }
.joinWithSeparator("&")
.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!


let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = bodyString.dataUsingEncoding(NSUTF8StringEncoding)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil else {
print(error?.localizedDescription)
return
}
guard let data = data else {
print("empty data")
return
}

// This is the HTML source of the landing page after login
let html = String(data: data, encoding: NSUTF8StringEncoding)
}
task.resume()
}

由于某些原因,我无法提出请求,直到我 disabled App Transport Security .该网站上的资源可能来自非 HTTPS 域。

关于ios - NSURLSession 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116022/

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