Example of using the HTTP / objective-C protocol of a synchronous HTTP request?

I can only find asynchronous HTTP / Object C HTTP examples. How to make a synchronous web request?

+7
objective-c synchronous
source share
3 answers

Agree with h4xxr and I will forward you to

http://allseeing-i.com/ASIHTTPRequest/

What a fantastic library that has robust HTTP request methods for synchronizing and asynchronously terminating with code samples.

+5
source
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL]; NSURLResponse * response = nil; NSError * error = nil; NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
+34
source

Depends on the data you use. Something simple like this is synchronous and useful from time to time:

 NSURL *url = [NSURL URLWithString:@"http://someaddress.asp?somedatarequest=1"]; NSArray *dataArray = [NSArray arrayWithContentsOfURL:url]; 

(Equivalent also exists for dictionaries)

In this case, the system will expect a response from someaddress.asp - therefore, it is best to put something like this in the background thread.

If you have control over the data format on the other end, this can be a quick and easy way to get data in the iPhone / iPad app ...

Change - you just need to specify the obvious, which is usually best asynchronously! You should not wait, connecting system resources, especially if the remote server has died, etc .... :)

+2
source

All Articles