我正在使用AFNetworking来拨打服务器。服务器已启用NTLM身份验证。我正在使用 setWillSendRequestForAuthenticationChallengeBlock 来处理该身份验证。
我的问题是,当我第一次打电话给服务器时,它完美无缺。但是,当我再次对服务器进行相同的调用时,我得到错误请求(400)错误。这是我的代码:
-(AFHTTPRequestOperation *)POST:(NSString *)URLString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[self.baseURL absoluteString],URLString]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil]]
;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.shouldUseCredentialStorage = NO;
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:success failure:failure];
[self addChallengeBlockForOperation:operation failure:^(NSError *error) {
failure(operation,error);
}];
//[self.operationQueue addOperation:operation];
[operation start];
return operation;
}
- (void)addChallengeBlockForOperation:(AFHTTPRequestOperation *)operation failure:(void (^)(NSError *error))failure {
[operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
if( [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM )
{
if( [challenge previousFailureCount] > 0 )
{
// Avoid too many failed authentication attempts which could lock out the user
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
else
{
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:[DataManager sharedInstance].fullUserName password:[DataManager sharedInstance].password persistence:NSURLCredentialPersistenceSynchronizable] forAuthenticationChallenge:challenge];
}
}
else
{
// Authenticate in other ways than NTLM if desired or cancel the auth like this:
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}];
}
我知道我可以通过将 text / html 添加到已接受的类型来删除此错误,但这不是解决方案。我也应该能够第二次获得JSON响应。我还得到 Stream xxxxx在第一次通话时在控制台上打开之前发送一个事件,在第二次通话时发送两次。
为了安全起见,我在日志中隐藏了URL。这是我的日志:
2015-12-03 16:14:04.854 iOS Docketing App[740:195426] *************Users/Authenticate/**************
2015-12-03 16:14:04.884 iOS Docketing App[740:195426] POST 'http:.....': {
"Content-Type" = "application/json; charset=utf-8";
} {
"userName" : "tkxel2"
}
2015-12-03 16:14:08.659 iOS Docketing App[740:195566] Stream 0x17f42a10 is sending an event before being opened
2015-12-03 16:14:09.473 iOS Docketing App[740:195426] 200 'http:.....' [4.5876 s]: {
"Cache-Control" = private;
"Content-Length" = 61;
"Content-Type" = "application/json; charset=utf-8";
Date = "Thu, 03 Dec 2015 11:14:13 GMT";
"Persistent-Auth" = false;
Server = "Microsoft-IIS/7.5";
Via = "1.1 vm-magagent.ntcorp.local, 1.1 vm-magrelay.skgf.com";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} {"practiceGroupName":"Egroup","role":"P","username":"tkxel2"}
2015-12-03 16:14:09.486 iOS Docketing App[740:195426] *************Users/Authenticate/**************
2015-12-03 06:14:09:478 iOS Docketing App[740:195426] __59-[NetworkHelper authenticateWithUserName:success:AndError:]_block_invoke Time: 4.619750 seconds Size: 104 bytes
2015-12-03 16:14:09.493 iOS Docketing App[740:195426] POST 'http:.....': {
"Content-Type" = "application/json; charset=utf-8";
} {
"userName" : "tkxel2"
}
2015-12-03 16:14:10.434 iOS Docketing App[740:195566] Stream 0x17a36010 is sending an event before being opened
2015-12-03 16:14:10.710 iOS Docketing App[740:195566] Stream 0x17b63ae0 is sending an event before being opened
2015-12-03 16:14:10.992 iOS Docketing App[740:195426] 400 'http:.....' [1.4990 s]: {
"Cache-Control" = private;
"Content-Length" = 1647;
"Content-Type" = "text/html";
Date = "Thu, 03 Dec 2015 11:14:14 GMT";
"Persistent-Auth" = false;
Server = "Microsoft-IIS/7.5";
Via = "1.1 vm-magagent.ntcorp.local, 1.1 vm-magrelay.skgf.com";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p>The server encountered an error processing the request. See server logs for more details.</p>
</div>
</body>
</html>