Swift - 基本http身份验证的身份验证挑战

时间:2017-10-10 05:43:05

标签: ios iphone swift

我尝试向swift中基本http authentification保护的服务器发出http请求,以获取针对iOS 10.3的应用。

我尝试将authentification标头添加到我的请求中:

var request = URLRequest(url: URL(string: "https://my.webservice.url")!)
request.setValue("Basic my_auth_header", forHTTPHeaderField: "Authorization")
NSLog("headers = %@", request.allHTTPHeaderFields!)

let task = URLSession.shared.dataTask(with: mutableRequest, completionHandler: {data, response, error -> () in
    do {
        let httpResponse = response as! HTTPURLResponse
        NSLog("response code %i", httpResponse.statusCode)
    }
})

在日志中,我可以在请求之前看到我的标题,但我总是得到401状态代码。

documentation about url request中我读到某些标题是保留的,不应修改,这些标题包括Authorization

我尝试实现URLSessionDelegate,特别是函数urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)。 但我不知道如何为基本的http身份验证创建凭据。

所以我的问题是,如何在swift中处理基本的http身份验证?

0 个答案:

没有答案
相关问题