2013-04-17 78 views
1

我发送请求,但不要调用connectionDidFinishLoading方法,为什么?connectionDidFinishLoading不被调用,为什么?

- (void)startHttpRequestWithCookie:(NSArray *)authCookies 
{ 
    NSURL *url = [NSURL URLWithString:@"http://test.test/mbox.php"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:authCookies]; 
    [request setHTTPShouldHandleCookies:NO]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setAllHTTPHeaderFields:headers]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    if (connection) { 
     // Create the NSMutableData to hold the received data. 
      responseData = [NSMutableData new]; 
    } else { 
     // Inform the user that the connection failed. 
    } 
} 

    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     [responseData setLength:0]; 
    } 

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [responseData appendData:data]; 
    } 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
     NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", responseString); 
     NSError *error = nil; 
     NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 

etc.. 
    } 

这是我的代码,我不明白为什么不叫connectionDidFinishLoading

非常感谢您的宝贵时间

+2

检查是否调用其他委托方法;连接可能失败?你有没有实现didFailWithError? –

+0

谢谢。错误“连接失败!错误 - 太多HTTP重定向错误域= NSURLErrorDomain代码= -1007”太多HTTP重定向“UserInfo = 0x7592ad0” – Alexander

回答

2

尝试实施- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response和检查响应对象返回的值。

相关问题