看起来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 报告 application/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;
}
这将同时返回 null[redirectResponse URL]
和redirectResponse
对象。就好像服务器端点根本没有返回 obj-c 代码的位置对象。这种行为有什么理由吗?