objective-c - NSURLConnection 收到错误时的不同结果

标签 objective-c nsurlconnection nsurlrequest

我正在使用 -[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]);
}

最佳答案

使用此委托(delegate)方法来处理此类错误。

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

关于objective-c - NSURLConnection 收到错误时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15543238/

相关文章:

objective-c - 在 macOS 上获取当前用户的桌面路径

objective-c - Watch OS 2 上的 NSURL 请求

ios - 如何从 URL 中删除查询(用于 GET 参数)?

objective-c - 解析 NSURL 的重定向?

ios - 在同步调用上强制NSUrlConnection超时

ios - push viewcontroller 是一个实例变量,pop 时不会调用 dealloc 方法

objective-c - Cocoa 应用程序在任何 GL 函数上提供 EXC_BAD_ACCESS

最接近枚举的 cocoa 当量

iphone - iOS教程中的xml解析

objective-c - NSURLConnection sendSynchronousRequest 的问题