2

我使用以下代码:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
NSLog(@"Received redirect Response: %@ %@", [response allHeaderFields], [NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]]);
return request;
}

当我收到带有以下标头数据的 302 时:

< HTTP/1.1 302 Found  
< Date: Wed, 03 Mar 2010 07:47:17 GMT  
< Server: lighttpd/1.4.19  
< Content-length: 0  
< Content-type: text/html;charset=utf-8  
< Location: `<new Location>`  
< Vary: Accept-Encoding  

这是 gdb 控制台中的输出:

2010-03-03 08:42:03.265 MyProg[68106:207] 收到重定向响应:(空)服务器错误 2010-03-03 08:42:14.414 MyProg[68106:207] 收到重定向响应:{
Connection = “Keep -活”;
“内容编码”= gzip;
“内容长度”= 20;
“内容类型”=“文本/html;字符集=utf-8”;
日期 =“格林威治标准时间 2010 年 3 月 3 日星期三 07:42:10”;
“保活”=“超时=15,最大值=100”;
位置 = " <new Location>";
服务器=“lighttpd/1.4.19”;
Vary = "接受编码"; } 成立

使用 Curl 时,我只得到一个响应,而 tracedump 也一样,所以我确信服务器只发送一个重定向。
为什么这个选择器被调用了两次?

4

1 回答 1

3

connection:willSendRequest:redirectResponse:在每个请求之前调用,因此在原始请求上调用一次,这不是重定向,因此响应为零;然后在加载重定向目标时调用它,其中响应是对初始请求的 302 响应。

于 2010-03-12T01:18:15.700 回答