Retrieve data from cocoa http get request

I implemented the following code:

NSURL *url = [ NSURL URLWithString:[ NSString stringWithFormat: @"http://www.google.com/search?q=%@", query ] ];
NSURLRequest *request = [ NSURLRequest requestWithURL: url ];

I want to extract the body from what I get back from the url above. I tried:

NSData *data = [request HTTPBody];

Does a data variable not return data? Am I going to extract the data from the query correctly?

Thanks!

+3
source share
3 answers

If you are just trying to get a web page, you can use this.

NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: @"http://www.google.com"] ]; 
NSString *test = [NSString stringWithContentsOfURL:url];

If you really want to convert data from NSData, you can use this:

NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+6
source

NSURLRequest simply defines the request - it does nothing on its own. To make a request, you need to send an NSURLConnection request .

, documentation, HTTPBody - , , .

+3

There is an article on www.eigo.co.uk that shows how to execute a request and get an answer in a string variable, but the piece of code that you need is ...

NSString * strResult = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

Check out the article here http://www.eigo.co.uk/iPhone-Submitting-HTTP-Requests.aspx

+1
source

Source: https://habr.com/ru/post/1705741/


All Articles