如何使用Alamofire 4.0发出NTML请求? [英] How to make a NTML request with Alamofire 4.0?
问题描述
这些是请求标头:
let headers: HTTPHeaders = [
"Accept": "application/json",
"username": "someUserName",
"password": "aPasswordForSomeUserName"
]
使用下面的代码发出请求时,它会给我最后的垃圾。但是,当我在线检查JSON解析器的响应时。这是有效的JSON。
When making a request with below code it's giving me "Garbage at the end". However, when I checked the response with JSON parser online. It's a valid 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
完成块时,它不是在抱怨未授权
,而是在抱怨不良的JSON(或一些垃圾) )。
What am I doing wrong? I believe, when using responseJSON
completion block it's not complaining about Unauthorization
, but it's complaining about bad JSON (or some garbage).
PS
推荐答案
我不知道这对您来说有多重要,但我有一个可行的解决方案,我会发布以供将来参考。
I don't know how relevant this is to you anymore but I've got a working solution I'll post for any future reference.
因此,我遇到了两个问题。第一个是重定向请求时,Authorization标头属于请求。第二个是未处理来自服务器的NTLM挑战。我希望下面的代码可以很容易地解释:)假设您将用户名和密码存储在变量名中。
So, I had two issues. The first one being that the Authorization header fell of the request when it was redirected. The second one being the NTLM-challenge from the server not being handled. The following code should be quite self explanatory I hope :) It asumes you store the username and password in variables name just that.
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
}
希望这对某人有帮助。
这篇关于如何使用Alamofire 4.0发出NTML请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!