gpt4 book ai didi

ios - 如何使用 Alamofire 4.0 发出 NTML 请求?

转载 作者:行者123 更新时间:2023-12-01 16:08:37 33 4
gpt4 key购买 nike

这些是请求 header :

    let headers: HTTPHeaders = [
"Accept": "application/json",
"username": "someUserName",
"password": "aPasswordForSomeUserName"
]

当使用下面的代码发出请求时,它给了我“最后的垃圾”。 但是,当我使用 JSON 解析器在线检查响应时。这是一个有效的 JSON。
Alamofire.request("http://myserver/list.svc/random", headers: headers).responseJSON { response in
print(response)
}

我也试过提出这样的请求 :
Alamofire.request("http://myserver/list.svc/random", headers: headers).responseString { response in
print(response)
}

我在控制台中收到此消息:“401 UNAUTHORIZED” .

我究竟做错了什么?我相信,当使用 responseJSON完成 block 它没有提示 Unauthorization ,但它提示糟糕的 JSON(或一些垃圾)。

附言相同的请求适用于 Advance Rest Client(一个 chrome 扩展)以及 chrome 浏览器。

最佳答案

我不知道这对您有多重要,但我有一个可行的解决方案,我将发布以供将来引用。

所以,我有两个问题。第一个是 Authorization header 在重定向时从请求中落下。第二个是来自未处理的服务器的 NTLM 挑战。我希望下面的代码应该很容易解释:) 它假设您将用户名和密码存储在变量名称中。

    let credentialData = "\(username):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
request.addValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")

let manager = Alamofire.SessionManager.default
let delegate: Alamofire.SessionDelegate = manager.delegate

// This bit will re-add the auth headers for the redirected request
delegate.taskWillPerformHTTPRedirection = { session, task, response, request in
var redirectedRequest = request

if let originalRequest = task.originalRequest, let redirectheaders = originalRequest.allHTTPHeaderFields {
if let authorizationHeaderValue = redirectheaders["Authorization"] {
redirectedRequest.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
}
if let contentTypeHeaderValue = redirectheaders["Content-Type"] {
redirectedRequest.setValue(contentTypeHeaderValue, forHTTPHeaderField: "Content-Type")
}
}

return redirectedRequest
}

// This bit looks at challenges received and applies the correct credentials
delegate.taskDidReceiveChallenge = { session, task, challenge in
var disposition: URLSession.AuthChallengeDisposition = .useCredential
var credential: URLCredential = URLCredential()

if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM) {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(user: username, password: password, persistence: URLCredential.Persistence.forSession)
}

if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
disposition = URLSession.AuthChallengeDisposition.useCredential
credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
}

return(disposition, credential)
}

manager.request(request).responseData { (response) in
// Handle response accordingly
}

希望这可以帮助某人。

关于ios - 如何使用 Alamofire 4.0 发出 NTML 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45912367/

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