gpt4 book ai didi

objective-c - AF 网络 2.0 : NSLocalizedDescription=Request failed: unacceptable content-type: text/html

转载 作者:太空狗 更新时间:2023-10-30 03:56:43 30 4
gpt4 key购买 nike

我尝试从 Apple 主页GET html。

AFHTTPRequestOperationManager *manager
= [AFHTTPRequestOperationManager manager];
[manager GET:@"http://www.apple.com/jp" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

这失败了,错误信息如下:

2014-02-07 14:54:22.922 Tests[1833:70b] Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a32650 {NSErrorFailingURLKey=http://www.apple.com/jp/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xea2e1a0> { URL: http://www.apple.com/jp/ } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Cache-Control" = "max-age=237";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 4402;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 07 Feb 2014 05:52:51 GMT";
Expires = "Fri, 07 Feb 2014 05:56:48 GMT";
Server = Apache;
Vary = "Accept-Encoding";
Via = "1.0 proxy1.screen.co.jp (squid)";
"X-Cache" = "MISS from proxy1.screen.co.jp";
"X-Cache-Lookup" = "HIT from proxy1.screen.co.jp:8080";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

我发现消息 Request failed: acceptable content-type: text/html 所以我明白我必须让 text/html 可接受。然后,我添加了以下行:manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

AFHTTPRequestOperationManager *manager 
= [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes
= [NSSet setWithObject:@"text/html"]; // ###### ADDED LINE ######
[manager GET:@"http://www.apple.com/jp" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

但这也失败了。

2014-02-07 14:55:48.927 Tests[1873:70b]
Error: Error Domain=NSCocoaErrorDomain Code=3840
"The operation couldn’t be completed. (Cocoa error 3840.)"
(JSON text did not start with
array or object and option to allow fragments not set.)
UserInfo=0xfa7b870 {NSDebugDescription=
JSON text did not start with
array or object and option to allow fragments not set.}

感谢您的帮助。

最佳答案

问题是 AFHTTPRequestOperationManager 默认为 AFJSONResponseSerializerresponseSerializer。通过更改 acceptableContentTypes,您将更改哪些内容类型将被接受,但您不会更改 AFJSONResponseSerializer 仍将在响应中查找 JSON 的事实。

您可能只想将 responseSerializer 设置为 AFHTTPResponseSerializer,这样它就不会再尝试解析 JSON(并且会自动接受 text/html ):

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

关于objective-c - AF 网络 2.0 : NSLocalizedDescription=Request failed: unacceptable content-type: text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21620429/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com