2013-04-17 80 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和檢查響應對象返回的值。

相關問題