来自 GET 请求的 AFNetworking 3.0 404 响应
AFNetworking 3.0 404 response from GET request
我正在尝试使用 AFNetowrking 发出 GET 请求。这是代码:
@property (nonatomic, readwrite) AFHTTPSessionManager *httpSessionManager;
...
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_httpSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseURL]
sessionConfiguration:config];
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
_httpSessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
_httpSessionManager.securityPolicy.allowInvalidCertificates = YES;
...
NSMutableDictionary* parameters = [self defaultUserSessionParameters];
NSString *url = [self constructURLWithEndpointPath:@"lead_sources.json"];
RequestSuccessBlock winBlock = ^(NSURLSessionDataTask *task, id responseObject)
{
NSLog(@"GetLeadSources, response Object: %@", [responseObject description]);
NSArray* leadSources = responseObject[@"lead_sources"];
if (!leadSources)
{
NSString* errorString = @"Successfully retrieved lead sources but no values were returned!";
NSLog(@"%@",errorString);
NSError* error = [NSError bpdErrorWithDescription:errorString];
completion(nil, error);
}
else
{
completion(leadSources,nil);
}
};
RequestFailureBlock failureBlock = ^(NSURLSessionDataTask *task, NSError *error)
{
NSLog(@"FAILURE: get lead sources, Error: %@", error);
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"server error response: %@ /end", ErrorResponse);
};
[self.httpSessionManager GET:url
parameters:parameters
progress:nil
success:winBlock
failure:failureBlock];
但我每次都收到这个错误:
Request failed: unacceptable content-type: text/html
我做了一些挖掘,发现这个错误是由于我的响应序列化程序而引发的,所以我添加了这一行来解决这个问题:
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
但是当我添加这个时,出现以下错误:
JSON text did not start with array or object and option to allow fragments not set.
为此我发现我需要设置阅读选项以允许片段。我尝试将序列化程序设置为:
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
但我又收到另一个错误:
Invalid value around character 0
我想我必须将响应序列化程序设置为 AFHTTPResponseSerializer,但我似乎无法弄清楚如何使用该序列化程序允许片段。
无论如何,我认为这不是抛出 404 的原因。我相信我没有击中我试图击中的域,并且 AFNetworking 在解析我正在击中的 url 抛出的响应时遇到问题。
所以两个问题:
如何才能让序列化程序正确解析我的响应? (也就是消除这三个错误?)
为什么抛出 404?我可以导航到它试图访问的url,但它说页面不可用
提前致谢!
您可以将响应序列化程序设置为 AFHTTPResponseSerializer,如下所示。但是,如果错误 404 是由于资源不再位于您提交的路径 url 中,唯一的解决方案是找出正确的 url 是什么。
_httpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
来自评论:
所以默认是 json 序列化器。但总的来说,我认为我的思考方式是不正确的。从我设置的服务器返回的响应都是 JSON,所以我应该使用默认值。我的问题是我没有到达终点,所以返回的响应是 HTML,所以它抛出了两个错误,一个是我们未能到达终点 (404),另一个是它无法解析text/html。第二个被抛出的原因是因为我没有达到终点,所以解决这个问题就解决了另一个问题。
我正在尝试使用 AFNetowrking 发出 GET 请求。这是代码:
@property (nonatomic, readwrite) AFHTTPSessionManager *httpSessionManager;
...
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_httpSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.baseURL]
sessionConfiguration:config];
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
_httpSessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
_httpSessionManager.securityPolicy.allowInvalidCertificates = YES;
...
NSMutableDictionary* parameters = [self defaultUserSessionParameters];
NSString *url = [self constructURLWithEndpointPath:@"lead_sources.json"];
RequestSuccessBlock winBlock = ^(NSURLSessionDataTask *task, id responseObject)
{
NSLog(@"GetLeadSources, response Object: %@", [responseObject description]);
NSArray* leadSources = responseObject[@"lead_sources"];
if (!leadSources)
{
NSString* errorString = @"Successfully retrieved lead sources but no values were returned!";
NSLog(@"%@",errorString);
NSError* error = [NSError bpdErrorWithDescription:errorString];
completion(nil, error);
}
else
{
completion(leadSources,nil);
}
};
RequestFailureBlock failureBlock = ^(NSURLSessionDataTask *task, NSError *error)
{
NSLog(@"FAILURE: get lead sources, Error: %@", error);
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"server error response: %@ /end", ErrorResponse);
};
[self.httpSessionManager GET:url
parameters:parameters
progress:nil
success:winBlock
failure:failureBlock];
但我每次都收到这个错误:
Request failed: unacceptable content-type: text/html
我做了一些挖掘,发现这个错误是由于我的响应序列化程序而引发的,所以我添加了这一行来解决这个问题:
_httpSessionManager.responseSerializer.acceptableContentTypes = [_httpSessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
但是当我添加这个时,出现以下错误:
JSON text did not start with array or object and option to allow fragments not set.
为此我发现我需要设置阅读选项以允许片段。我尝试将序列化程序设置为:
_httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
但我又收到另一个错误:
Invalid value around character 0
我想我必须将响应序列化程序设置为 AFHTTPResponseSerializer,但我似乎无法弄清楚如何使用该序列化程序允许片段。
无论如何,我认为这不是抛出 404 的原因。我相信我没有击中我试图击中的域,并且 AFNetworking 在解析我正在击中的 url 抛出的响应时遇到问题。
所以两个问题:
如何才能让序列化程序正确解析我的响应? (也就是消除这三个错误?)
为什么抛出 404?我可以导航到它试图访问的url,但它说页面不可用
提前致谢!
您可以将响应序列化程序设置为 AFHTTPResponseSerializer,如下所示。但是,如果错误 404 是由于资源不再位于您提交的路径 url 中,唯一的解决方案是找出正确的 url 是什么。
_httpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
来自评论:
所以默认是 json 序列化器。但总的来说,我认为我的思考方式是不正确的。从我设置的服务器返回的响应都是 JSON,所以我应该使用默认值。我的问题是我没有到达终点,所以返回的响应是 HTML,所以它抛出了两个错误,一个是我们未能到达终点 (404),另一个是它无法解析text/html。第二个被抛出的原因是因为我没有达到终点,所以解决这个问题就解决了另一个问题。