views:

370

answers:

4

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. :)

A: 

If there is WSDL involved it is probably a SOAP web service so it probably expects some SOAPy XML as input, and I don't think you are doing that. Take a look at this question maybe: http://stackoverflow.com/questions/204465/how-to-access-soap-services-from-iphone

MK
A: 

You're setting values in the HTTP header, not GET or POST parameters in the HTTP request. That's probably not what you want to do.

If the server accepts parameters in GET requests, you might be able to do something like this in the URL:

"http://myWebService/function?location=USA&amp;module=DEVELOPMENT"

Failing that, you'll have to go the full SOAP route as MK said.

David Gelhar
thanks, I'm gonna try this first n.n
Alejandra Meraz
@Alejandra note: I just corrected a typo in the URL string: should be a "?" before the parameters, not a ";" as I had
David Gelhar
not working, seems I'm need to use SOAP.
Alejandra Meraz
A: 

some examples about GET Post SOAP

GET request

GET /index.html?userid=joe&password=guessme HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0

Post request

POST /login.jsp HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userid=joe&password=guessme

Soap Request

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"&gt;

<soap:Body xmlns:m="http://www.example.org/stock"&gt;
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>

the HOST,User-Agent,Content-Length,Content-Type are items in the Request Header

owen
A: 

Ok. i modified the request to use SOAP and it looks like this.

NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;\n"
     "<soap:Body>\n"
     "<function xmlns=\"http://tempuri.org/\" />\n"
     "</soap:Body>\n"
     "</soap:Envelope>\n"];

        NSURL *url = [NSURL URLWithString:@"http://myHost.com/myWebService/service.asmx"]; //the url to the WSDL

        NsMutableURLRequest theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
     NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];

     [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
     [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Lenght"];
     [theRequest setHTTPMethod:@"POST"];
     [theRequest addValue:@"myhost.com" forHTTPHeaderField:@"Host"];
     [theRequest addValue:@"http://tempuri.org/function" forHTTPHeaderField:@"SOAPAction"];
     [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
     theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

I basically copy and modified the soap request the web service gave as an example.
But this needs the location and module parameters modification. I tried modifying the soapMessage like this:

    NSString *soapMessage = [NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;\n"
 "<soap:Body xmlns=\"http://tempuri.org/\" />\n"
    "<m:GetMonitorList>\n"
    "<m:location>USA</m:location>\n"
    "</m:GetMonitorList>\n"
 "</soap:Body>\n"
 "</soap:Envelope>\n"];

But is not working...any thoughts how should I modify it?

Alejandra Meraz
it seems to be working. But the webservice return nothing. during the connection, the method didReceiveResponse execute once and the didFinishLoading method executes. But not even once the method didReceiveData.I wonder if, even though there is no USA locations, it will still send at least something?
Alejandra Meraz