如何获得IOS的响应体?

时间:2011-12-07 05:38:01

标签: ios connection nsurlconnection response

现在我想得到回复正文。我是IOS开发人员中的新人。我只知道我可以使用response.statusCode来获取httpstatuscode,例如400,500等等。但如何获得反应机构。也许allheaderFileds或数据或error.Description?

1 个答案:

答案 0 :(得分:1)

有关详细信息,请访问:URL Loading System Programming Guide

您需要一个作为NSURLConnection代表的对象。你需要有一个receivedData成员变量,你需要实现委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}