【发布时间】:2014-02-28 11:15:41
【问题描述】:
我正在构建一个应该返回 JSON 对象的简单 GET 函数。使用 AFNetworking 2. 代码可以编译,但我无法确定运行时错误的来源。 难道服务器上的 content-type: 是 text/html 并且我期望 jsonobj. 服务器使用“基本用户:id”标头,我应该将它添加到 setValue forHttpHeaderField 吗?
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://the website.com"]];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"pass"];
[manager GET:@"/api/data/interval?month" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@" Yep %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@" Nope %@", error);
}];
这是我得到的错误
Nope Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: forbidden (403)" UserInfo=0x8a9dec0 {NSErrorFailingURLKey=http://thewebsite.com/api/data/interval?month, NSLocalizedDescription=Request failed: forbidden (403), NSUnderlyingError=0x8a99e00 "Request failed: unacceptable content-type: text/html", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8bb0ac0> { URL: http://thewebsite.com/api/data/interval?month } { status code: 403, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Language" = "";
"Content-Type" = "text/html";
Date = "Tue, 04 Feb 2014 13:02:12 GMT";
【问题讨论】:
-
检查您的网址
http://thewebsite.com/api/data/interval?month -
检查您的网址,因为错误表明,您尝试访问的内容被禁止
标签: ios get afnetworking-2