NSURLConnection 成功但没有 HTTP 响应

发布于 2024-09-08 18:38:59 字数 1760 浏览 4 评论 0原文

猜谜语:我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2024-09-15 18:38:59

我能够通过添加重定向委托来解决问题,该委托始终接受

-(NSURLRequest *)connection:(NSURLConnection *)connection
            willSendRequest:(NSURLRequest *)request
           redirectResponse:(NSURLResponse *)redirectResponse
{
    return request;
}

据我所知,这是默认行为,所以我不知道为什么这可以解决问题。

我向其发送请求的 URL 确实发出了重定向,但这也无法解释为什么 HTTP 请求在其他 ~95% 的时间内有效。

我希望这对将来遇到类似问题的人有所帮助。

I was able to fix the problem by adding the delegate for re-directs which always accepts

-(NSURLRequest *)connection:(NSURLConnection *)connection
            willSendRequest:(NSURLRequest *)request
           redirectResponse:(NSURLResponse *)redirectResponse
{
    return request;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文