Objective c 获取错误时,NSURLConnection会产生不同的结果

Objective c 获取错误时,NSURLConnection会产生不同的结果,objective-c,nsurlconnection,nsurlrequest,Objective C,Nsurlconnection,Nsurlrequest,我正在使用-[NSURLConnection initWithRequest…]连接到服务器, 当服务器端出现故障时,它将以错误400响应,并在正文中显示一条消息。 当没有错误时,我可以在didReceiveData中获取响应数据。但是当服务器返回错误400时,我在didReceiveData中没有收到任何错误消息 但是,如果我使用-[NSURLConnection sendSynchronousRequest…]执行相同的请求 我确实从sendSynchronousRequest的返回值中获取

我正在使用
-[NSURLConnection initWithRequest…]
连接到服务器, 当服务器端出现故障时,它将以错误400响应,并在正文中显示一条消息。 当没有错误时,我可以在didReceiveData中获取响应数据。但是当服务器返回错误400时,我在didReceiveData中没有收到任何错误消息

但是,如果我使用
-[NSURLConnection sendSynchronousRequest…]
执行相同的请求 我确实从
sendSynchronousRequest
的返回值中获取了错误数据

我想从didReceiveData获取错误消息,但不知道为什么无法获取。 你和我有什么不同,有人能帮我吗

这样我可以得到错误消息:

NSURL *url = [NSURL URLWithString:@"http://devapi.xxx.com/v1/debug/error"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                          timeoutInterval:240] autorelease];

[request setHTTPMethod:@"POST"];


[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
                                     returningResponse:&response
                                                 error:&error];


NSLog(@"STATUS:%d", [((NSHTTPURLResponse *)response) statusCode]);
NSLog(@"ERROR:%@", error);
这样我就得不到了:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://devapi.xxx.com/v1/debug/error"]
                                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                         timeoutInterval:240] autorelease];

[request setHTTPMethod:@"POST"];


[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
urlConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"statusCode:%d",((NSHTTPURLResponse *)response).statusCode);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
NSLog(@"DATA:%@",[NSString stringWithCString:[receivedData bytes] encoding:NSASCIIStringEncoding]);
}

使用此委托方法处理此类错误

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
      NSLog(@" Failed with error---%@",error);
}

我想我找到了发生的事情, 当我在didReceiveResponse中获得错误状态代码时, 我立即尝试获取数据并取消连接, 但这是错误的, 因为didReceiveData在didReceiveResponse之后仍在接收中


非常感谢

成功(非400)响应是否调用
didReceiveData
didFailWithError
是否调用过?能否显示日志输出的状态、数据和错误?他的问题是,当HTTP代码不是200时,他无法始终从服务器读取响应的“内容”部分。这是一个问题,因为它是在哪里写的,它是哪种类型的应用程序错误。