NSURLResponse未在标题中显示位置

时间:2013-03-18 14:46:46

标签: ios

看起来NSURLResponse正在修改我正在回复的响应标题中的位置。如果我运行curl命令,我会得到:

< HTTP/1.1 302 Found
< Date: Mon, 18 Mar 2013 14:43:41 GMT
< Server: Apache/2.2.14 (Ubuntu)
< X-Powered-By: PHP/5.3.14 ZendServer/5.0
< Set-Cookie: PHPSESSID=kv1j4cjpt69q91sgd2m270d775; path=/; secure; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Location: http://kt-act.s3.amazonaws.com/...
< Vary: Accept-Encoding
< Content-Length: 0
< Connection: close
< Content-Type: text/html

但是,使用以下Obj-C代码命中相同的端点:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/content",kBaseAPIURL,_document.href]]];
[NSURLConnection connectionWithRequest:request delegate:self];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_resourceData setLength:0];
_progressView.progress=0.0f;
_loadingView.hidden=NO;
fileSize = [[NSNumber numberWithLongLong:[response expectedContentLength]] intValue];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSLog(@"Response: %@", headers);
}

给我这个:

2013-03-18 10:40:11.537 KTMobile[25221:1ac03] Response: {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = close;
"Content-Length" = 36;
"Content-Type" = "application/json";
Date = "Mon, 18 Mar 2013 14:38:44 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = "Apache/2.2.14 (Ubuntu)";
"Www-Authenticate" = "Basic realm=\"Protected\"";
"X-Powered-By" = "PHP/5.3.14 ZendServer/5.0";
"X-Response-Time" = "0.153447";
}

有谁知道为什么会失去位置标题?为什么curl会报告text / html但是NSURLResponse报告应用程序/ json返回?我也尝试按照另一个SO线程中的建议获取响应URL的查询/参数,但是仍然没有给我这个位置。

编辑: 我也尝试了connection:willSendRequest:redirectResponse代表。

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
NSLog(@"Redirect response url! %@", [redirectResponse URL]);
NSLog(@"Redirect response! %@", redirectResponse);

return request;
}

这将为[redirectResponse URL]redirectResponse对象返回null。就像服务器端点根本没有返回obj-c代码的位置对象一样。这种行为有什么理由吗?

1 个答案:

答案 0 :(得分:0)

“HTTP 302”是一个重定向响应,“Location”标题提供了应该加载资源的新URL。

NSURLConnection透明地遵循重定向,NSURLResponse是重定向请求的响应。

如果您想了解重定向信息,我认为您可以实施connection:willSendRequest:redirectResponse:,但我从未尝试过这样做。

相关问题