How to receive NSURLResponse message

I am making an http request to a web server as follows:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://server.com"]]; [request setHTTPMethod: @"POST"]; [request setHTTPBody: myRequestData]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 

But how can I get a response from the server and parse it? Thank you very much:)

+4
source share
3 answers

Since you assign the response to the returnData instance returnData , convert it to a string for starters, just see what you get, the parsing can be done either using NSXMLParser or with some JSON library depending on the response format.

Here you can convert the response to a string:

 NSString *responseBody = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
+23
source

I assume you mean the full NSURLResponse so you can check for headers and the like. In this case, you need to pass the parameter returnResponse, this is an output parameter of the type (NSURLResponse **). See docs .

+2
source

returnData is your answer. If its xml, you can use NSXMLParser to parse it if its json uses JSONFramework.

+2
source