Duaan
Duaan

Reputation: 609

NSURLConnection GET REQUEST

I came across NSURLConnection, I used it before, simply on request, and getting data and parsing it. However this time web developer has developed GET and POST requests.

I want through many tutorials and stack question and tried to get desired result. As I see there is sometime request, like this

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL"]
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10];

[request setHTTPMethod: @"GET"];

NSError *requestError;
NSURLResponse *urlResponse = nil;


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

also few others I have seen.

I looks easy but I am unable to find what is required for any POST and GET request.

The data which I have received from my web developer is

SOAP 1.2


POST /DEMOService/DEMO.asmx HTTP/1.1
Host: projects.demosite.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

and in return there will be GET and POST

The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.

     GET /DEMOService/DEMO.asmx/VerifyLogin?username=string&password=string&AuthenticationKey=string HTTP/1.1


    Host: projects.demosite.com

I am well-aware of delegates of NSURLConnections, which are following..

#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}

THE ONLY THING WHERE I AM STUCK IS

How to write request where I have pass arguments, in GET or POST request.

Thanks

Upvotes: 2

Views: 11561

Answers (1)

Stuart M
Stuart M

Reputation: 11588

If your arguments are being sent in the URL itself (e.g., as part of the URL path or query string), then you just need to include them in the NSURL argument. For instance, you might have the following:

NSString *urlString = [NSString stringWithFormat:@"https://hostname/DEMOService/DEMO.asmx/VerifyLogin?username=%@&password=%@&AuthenticationKey=%@",
                       username,
                       password,
                       authenticationKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10];

where username, password, and authenticationKey are local variables you set elsewhere.

Your response from the server is stored by the data contained in the NSData instance returned by -[NSURLConnection sendSynchronousRequest:returningResponse:error:].

So in your example, your response above would be stored in the response1 variable. And you can convert this to a string and/or parse it as needed.

Upvotes: 7

Related Questions