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
非常感謝您的寶貴時間
檢查是否調用其他委託方法;連接可能失敗?你有沒有實現didFailWithError? –
謝謝。錯誤「連接失敗!錯誤 - 太多HTTP重定向錯誤域= NSURLErrorDomain代碼= -1007」太多HTTP重定向「UserInfo = 0x7592ad0」 – Alexander