How to send a Get request in iOS?

Sanchit Paurush picture Sanchit Paurush · Jun 2, 2011 · Viewed 65.8k times · Source

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it shows an exception of unrecognized selector send in [NSURLRequest setHTTPMethod:] I am setting it as

[requestObject setHTTPMethod:@"GET"];

Tell me what could be the problem. Also provide me the code if you have.

Answer

NSMutableURLRequest *request = 
[NSMutableURLRequest requestWithURL:[NSURL 
            URLWithString:serverAddress] 
            cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10
 ];

[request setHTTPMethod: @"GET"];

NSError *requestError = nil;
NSURLResponse *urlResponse = nil;


NSData *response1 =
        [NSURLConnection sendSynchronousRequest:request
                         returningResponse:&urlResponse error:&requestError];