I can only find asynchronous iPad/objective C HTTP examples. How do I do a synchronous web request?
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:aURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
Agree with h4xxr and I would forward you to
http://allseeing-i.com/ASIHTTPRequest/
Which is a fantastic lib that has robust HTTP request methods for both synch and asynch complete with code samples.
Depends on what data you're after. Something simple like this is synchronous, and is handy 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 wait for a response from someaddress.asp - therefore best perhaps to put something like this into a background thread.
If you have control over the format of the data at the other end, this can be a quick and easy way to get data into an iPhone/iPad app...
Edit - just wanted to state the obvious that typically asynchronous is usually best! No waiting around tying up system resources, especially if remote server has died etc... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With