现在,我想获取响应正文。我是IOS开发人员的新手。我只知道我可以使用response.statusCode来获取httpstatuscode,例如400,500等等。但是如何获得反应体。也许是allheaderFileds或数据或错误。说明?

最佳答案

有关详细信息,请访问: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];
}

关于ios - 如何在IOS中获得响应体?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8410882/

10-10 02:37