为什么我收到 1 个 302 响应时会得到两个重定向响应?
我使用以下代码:
- (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] 收到重定向响应:{
连接=“保持活动”;
“内容编码”= gzip;
“内容长度”= 20;
"Content-Type" = "text/html;charset=utf-8";
日期 = "2010 年 3 月 3 日星期三 07:42:10 GMT";
“保持活动”=“超时=15,最大值=100”;
Location = "<新位置>
";
服务器=“lighttpd/1.4.19”;
Vary = "接受编码"; } 发现
当使用 Curl 时,我只得到一个响应,并且 Tracedump 也给出相同的信息,因此我确信服务器只发送一个重定向。
为什么这个选择器被调用两次?
I use the following code:
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
NSLog(@"Received redirect Response: %@ %@", [response allHeaderFields], [NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]]);
return request;
}
When I receive a 302 with the following header data:
< 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
this is the output in gdb console:
2010-03-03 08:42:03.265 MyProg[68106:207] Received redirect Response:
(null) server error 2010-03-03 08:42:14.414 MyProg[68106:207]
Received redirect Response: {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 20;
"Content-Type" = "text/html;charset=utf-8";
Date = "Wed, 03 Mar 2010 07:42:10 GMT";
"Keep-Alive" = "timeout=15, max=100";
Location = "<new Location>
";
Server = "lighttpd/1.4.19";
Vary = "Accept-Encoding"; } found
When using Curl I only get one response and tracedump tells the same, so I am sure that the server sends only one redirect.
Why is this selector called twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
connection:willSendRequest:redirectResponse:
在每个请求之前被调用,因此它在原始请求上调用一次,这不是重定向,因此响应为零;然后在加载重定向目标时调用它,其中响应是对初始请求的 302 响应。connection:willSendRequest:redirectResponse:
gets called before every request, so it is called once on the original request, which was not a redirect so response is nil; then it gets called when loading the redirection target, where response is the 302 response to the initial request.