NSURLConnection 成功但没有 HTTP 响应
猜谜语:我在 iPhone Simulator SDK 3.1.2 对象上使用 NSURLConnection 来发出多个连续(非并发)https 请求。 95% 的时间一切都工作正常,但每隔一段时间(可能大约 50-100 个请求)我会遇到这样的情况:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
在 [connection start] 之后调用委托方法,但没有任何对委托方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
我已将问题简化为以下来源。据我了解,这违反了 NSURLConnection 定义行为的契约。有其他人遇到过这个问题吗?这是一个已知的错误还是我使用 NSURLConnection 错误?
static BOOL hasSeenResponse = NO;
//Create a NSURLConnection and start it
-(void) begin {
NSURL* url = [NSURL URLWithString@"https://some.domain.com/some/path/?some=query"];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
if ([NSURLConnection canHandleRequest:request]) {
NSURLConnection* connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
hasSeenResponse = NO;
[connection start];
}
}
#pragma mark NSURLConnection Delegate Methods
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (!hasSeenResponse) {
NSLog(@"\n\nNo Response Recieved\n");
}
[connection release];
[self begin];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
[self begin];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
hasSeenResponse = YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
Riddle me this: I am using a NSURLConnection on an iPhone Simulator SDK 3.1.2 object to issue multiple sequential (not concurrent) https requests. Everything works fine 95% of the time, but every so often (maybe around 50-100 requests) I get the situation where the delegate method
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
is called after [connection start], but without any call to the delegate method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
I have reduced the problem down to the source below. From what I understand this breaks the contract of NSURLConnection's defined behaviour. Has anyone else had this problem, and is it a known bug or am I using NSURLConnection wrong?
static BOOL hasSeenResponse = NO;
//Create a NSURLConnection and start it
-(void) begin {
NSURL* url = [NSURL URLWithString@"https://some.domain.com/some/path/?some=query"];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
if ([NSURLConnection canHandleRequest:request]) {
NSURLConnection* connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
hasSeenResponse = NO;
[connection start];
}
}
#pragma mark NSURLConnection Delegate Methods
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (!hasSeenResponse) {
NSLog(@"\n\nNo Response Recieved\n");
}
[connection release];
[self begin];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
[self begin];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
hasSeenResponse = YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过添加重定向委托来解决问题,该委托始终接受
据我所知,这是默认行为,所以我不知道为什么这可以解决问题。
我向其发送请求的 URL 确实发出了重定向,但这也无法解释为什么 HTTP 请求在其他 ~95% 的时间内有效。
我希望这对将来遇到类似问题的人有所帮助。
I was able to fix the problem by adding the delegate for re-directs which always accepts
As I understand it, this is the default behavior so I do not know why this solves the problem.
The URL I was sending the request to, did issue a re-direct, but that also does not explain why the HTTP request worked the other ~95% of the time.
I hope this helps someone with a similar problem in the future.