我使用AFHTTPSessionManager通过授权标头获取GET请求;
如果错误的用户/密码服务器根据邮递员返回内容类型:text / html with:
<!DOCTYPE html>
<html>
<head>
<title>Apache Tomcat/8.0.21 - Error report</title>
<style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style>
</head>
<body>
<h1>HTTP Status 401 - Błąd podczas autentykacji</h1>
<div class="line"></div>
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u>Błąd podczas autentykacji</u>
</p>
<p>
<b>description</b>
<u>This request requires HTTP authentication.</u>
</p>
<hr class="line">
<h3>Apache Tomcat/8.0.21</h3>
</body>
</html>
不幸的是NSURLSessionDataTask返回
NSHTTPURLResponse *test = (NSHTTPURLResponse *)task.response;
NSLog (@"Status code, %i", test.statusCode);
返回状态代码,500
以及NSError
NSString* errorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"Error response: %@", errorResponse);
返回错误回复:{&#34;错误&#34;:{&#34;代码&#34;:&#34; UNKNOWN_ERROR&#34;,&#34;详细信息&#34;:null,&#34 ;描述&#34;:&#34;服务器错误&#34;,&#34;版本&#34;:0}}
这是我的获取操作方法。
- (void)getOperationForAction:(NSString *)action
parameters:(NSArray *)params
completion:(id (^)(id json, BOOL success))completion {
NSURL *url = [self apiUrlForAction:action params:params.mutableCopy];
AFHTTPSessionManager *manager = [self sessionManager];
[manager GET: url.absoluteString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"JSON: %@", responseObject);
if (completion) {
completion(responseObject, YES);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSString *responseString = [[NSString alloc] initWithData:(NSData *)error.userInfo[NSLocalizedRecoverySuggestionErrorKey] encoding:NSUTF8StringEncoding];
if(responseString != nil && ![responseString isEqualToString: @""]) {
NSData *JSONData = [responseString dataUsingEncoding: NSUTF8StringEncoding];
id responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options: NSJSONReadingMutableContainers error:nil];
if(responseJSON != nil) {
}
}
NSHTTPURLResponse *test = (NSHTTPURLResponse *)task.response;
NSLog (@"Status code, %i", test.statusCode);
NSString* errorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"Error response: %@", errorResponse);
ApiError *apiError = [[ApiError alloc] initWithString:errorResponse error:nil];
completion(apiError, NO);
}];
}
请求处理正确 - 当我故意使用错误的身份验证登录/密码来检查响应错误时出现401问题。问题在于前面提到的状态代码不正确。
@Edit 为了
NSHTTPURLResponse *test = (NSHTTPURLResponse *)task.response;
NSLog (@"Status code, %i", test.statusCode);
NSLog (@"Error code, %i", error.code);
NSLog (@"Error domain, %@", error.domain);
状态代码,500 错误代码,-1011 错误域,com.alamofire.error.serialization.response