【问题标题】:NSURLConnection different result when get errorNSURLConnection 得到错误时的不同结果
【发布时间】:2013-03-21 09:07:24
【问题描述】:

我正在使用-[NSURLConnection initWithRequest...] 连接到服务器, 当服务器端失败时,它会以错误 400 响应,并在正文中显示一条消息。 在没有错误的情况下,我可以在 didReceiveData 中获取响应数据。但是当服务器返回错误 400 时,我在 didReceiveData 中没有收到任何错误消息。

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

我想从 didReceiveData 中获取错误消息,但我不知道为什么我无法得到它。 do之间有什么不同,有人可以帮助我吗?

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

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]);
}

【问题讨论】:

  • 成功(非400)响应是否调用didReceiveDatadidFailWithError 有人打电话吗?
  • 您能否显示您的状态、数据和错误的日志输出?
  • 他的问题是,当HTTP代码不是200时,他不能总是从服务器读取响应的“内容”部分。这是一个问题,因为它是写哪一种应用程序错误的地方。

标签: objective-c nsurlconnection nsurlrequest


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    非常感谢!

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2020-03-01
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多