I'm writing an application that connect with a server using NSURLConnection
.
我正在编写一个使用NSURLConnection连接服务器的应用程序。
In the delegate method didreceiveresponse
, if the status code is 404, I cancel the connection and I would like to show a message with a custom error that is generated in the server.
在委托方法didreceive响应,如果状态代码是404,我取消连接,我想显示一条消息,其中包含在服务器中生成的自定义错误。
The problem is that from response object, I only can get statuscode, headers, mimetype, etc. but no body.
问题是,从响应对象,我只能得到状态码,标题,mimetype等,但没有身体。
How do I get the body message from NSURLResponse
?
如何从NSURLResponse获取正文消息?
1 个解决方案
#1
26
Why do you cancel the connection? After all, 404 can have content body as well. Just don't cancel it, and let the program call the next delegate NSURLConnection method. When the data [the content body] is sent - (void)connection:(NSURLConnection *) didReceiveData:(NSData *)
is called, you need to retrieve the data there. Read corresponding part in the docs:
为什么要取消连接?毕竟,404也可以拥有内容正文。只是不要取消它,让程序调用下一个委托NSURLConnection方法。当数据[内容体]被发送 - (void)连接:(NSURLConnection *)didReceiveData:(NSData *)被调用时,需要在那里检索数据。阅读文档中的相应部分:
The response from a server to a request can be viewed as two parts: metadata describing the contents and the URL content data. The metadata that is common to most protocols is encapsulated by the NSURLResponse class and consists of the MIME type, expected content length, text encoding (where applicable), and the URL that provided the response.
从服务器到请求的响应可以被视为两部分:描述内容的元数据和URL内容数据。大多数协议通用的元数据由NSURLResponse类封装,包含MIME类型,预期内容长度,文本编码(如果适用)以及提供响应的URL。
The NSURLConnection and NSURLDownload classes provide the interface to make a connection specified by an NSURLRequest object and download the contents. An NSURLConnection object provides data to the delegate as it is received from the originating source, whereas an NSURLDownload object writes the request data directly to disk. Both classes provide extensive delegate support for responding to redirects, authentication challenges, and error conditions.
NSURLConnection和NSURLDownload类提供了用于建立NSURLRequest对象指定的连接并下载内容的接口。 NSURLConnection对象在从原始源接收时向委托提供数据,而NSURLDownload对象将请求数据直接写入磁盘。这两个类都为响应重定向,身份验证挑战和错误条件提供了广泛的委托支持。
As for an example delegate implementation:
至于代表实现的示例:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{
NSLog(@"String sent from server %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);
}
#1
26
Why do you cancel the connection? After all, 404 can have content body as well. Just don't cancel it, and let the program call the next delegate NSURLConnection method. When the data [the content body] is sent - (void)connection:(NSURLConnection *) didReceiveData:(NSData *)
is called, you need to retrieve the data there. Read corresponding part in the docs:
为什么要取消连接?毕竟,404也可以拥有内容正文。只是不要取消它,让程序调用下一个委托NSURLConnection方法。当数据[内容体]被发送 - (void)连接:(NSURLConnection *)didReceiveData:(NSData *)被调用时,需要在那里检索数据。阅读文档中的相应部分:
The response from a server to a request can be viewed as two parts: metadata describing the contents and the URL content data. The metadata that is common to most protocols is encapsulated by the NSURLResponse class and consists of the MIME type, expected content length, text encoding (where applicable), and the URL that provided the response.
从服务器到请求的响应可以被视为两部分:描述内容的元数据和URL内容数据。大多数协议通用的元数据由NSURLResponse类封装,包含MIME类型,预期内容长度,文本编码(如果适用)以及提供响应的URL。
The NSURLConnection and NSURLDownload classes provide the interface to make a connection specified by an NSURLRequest object and download the contents. An NSURLConnection object provides data to the delegate as it is received from the originating source, whereas an NSURLDownload object writes the request data directly to disk. Both classes provide extensive delegate support for responding to redirects, authentication challenges, and error conditions.
NSURLConnection和NSURLDownload类提供了用于建立NSURLRequest对象指定的连接并下载内容的接口。 NSURLConnection对象在从原始源接收时向委托提供数据,而NSURLDownload对象将请求数据直接写入磁盘。这两个类都为响应重定向,身份验证挑战和错误条件提供了广泛的委托支持。
As for an example delegate implementation:
至于代表实现的示例:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{
NSLog(@"String sent from server %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);
}