NSURLSession 认证

NSURLSession Authentication

我尝试使用找到的 swift 代码 here on the website found here,但响应是 html 代码,有两个错误:"You must enter a password!" 和 "You must enter a User name!" 我我是 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. 不要关闭此控制台。返回网站并像往常一样输入您的用户名和密码。

观察页面发送什么数据:

纯文本:

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 { [=11=] + "=" +  }
                        .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 域。