我正在使用 AFHTTPSessionManager 获取带有授权标头的请求;
如果根据邮递员的错误用户/密码服务器返回内容类型:text/html,其中:
<!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);
返回错误响应:{"error":{"code":"UNKNOWN_ERROR","details":null,"description":"Server error","version":0}}
这是我的get操作方法。
- (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 问题。问题在于前面提到的不正确的状态代码。
@编辑为
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