Objective-c – Send parameters to a web service

iphone, objective-c, parameters, web-services

Before I start: I'm programming for Iphone, using objective C.

I have already implemented a call to a web service function using NSURLRequest and NSURLConnection. The function then returns a XML with the info I need.

The code is as follows:

NSURL *url = [NSURL URLWithString:@"http://myWebService/function"];NSMutableURLRequest theRequest = [[NSMutableURLRequest alloc] initWithURL:url];NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

i also implemented the methods

  • didRecieveResponse
  • didRecieveAuthenticationChallenge
  • didRecievedData
  • didFailWithError
  • connectionDidFinishLoading.

And it works perfectly.

Now I need to send 2 parameters to the function: "location" and "module".
I tried using the following modification:

NSMutableURLRequest theRequest   = [[NSMutableURLRequest alloc] initWithURL:url];[theRequest setValue:@"USA" forHTTPHeaderField:@"location"];[theRequest setValue:@"DEVELOPMENT" forHTTPHeaderField:@"module"];NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

But it doesn't seem to work.
I'm doing something wrong? is there a way to know if I'm using the wrong names for the parameters (as maybe it is "Location" or "LOCATION" or it doesn't matter?)?
or a way to know which parameters is the function waiting for…

Extra info:
I don't have access to the source of the web service so I can't modify it.
But I can access the WSDL. The person who made the function say is all there… but I can't make any sense of it >.<…

Any help would be appreciated. 🙂

Best Solution

Took me a couple of hours to get this working so I thought I'd post my solution so it saves someone else some time in the future. This is how I got objective-c to retrieve JSON data from a .NET WCF service with a string parameter.

(I used wireshark to capture a .net client talking to the wcf service so I knew what I needed to pass from my iOS client)

static NSString *feedURLString = @"http://www.mydomain.com/DownloadService.svc";NSMutableURLRequest *faURLRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];[faURLRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];[faURLRequest setHTTPMethod:@"POST"]; [faURLRequest addValue:@"http://tempuri.org/IDownloadService/GetChanges" forHTTPHeaderField:@"SOAPAction"];NSString *localLastSync;if (userProfile.lastSync == nil){    localLastSync = @"2011-09-01T12:00:00";}else{    localLastSync = userProfile.lastSync;}NSString *soapMessage = [NSString stringWithFormat:                         @"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"                         "<s:Body >\n"                         "<GetChanges xmlns=\"http://tempuri.org/\">\n"                         "<lastSync>%@</lastSync>\n"                         "</GetChanges>\n"                         "</s:Body>\n"                         "</s:Envelope>\n", localLastSync];[faURLRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];self.downloadConnection =[[NSURLConnection alloc] initWithRequest:faURLRequest delegate:self];