NSURLConnection成功,没有HTTP响应

时间:2010-07-08 08:10:45

标签: cocoa https ios-simulator

在我这里说道:我在 iPhone模拟器SDK 3.1.2 对象上使用 NSURLConnection 来发出多个顺序(非并发) https请求。一切都在95%的时间内正常工作,但每隔一段时间(可能大约50-100个请求)我得到委托方法的情况

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
在[连接开始]之后调用

,但是没有对委托方法的任何调用

- (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 {
}

1 个答案:

答案 0 :(得分:0)

我能够通过添加总是接受

的重定向代理来解决问题
-(NSURLRequest *)connection:(NSURLConnection *)connection
            willSendRequest:(NSURLRequest *)request
           redirectResponse:(NSURLResponse *)redirectResponse
{
    return request;
}

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

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

我希望这可以帮助将来遇到类似问题的人。