开发者

Send parameters to a web service

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 sour开发者_运维百科ce 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. :)


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];


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&module=DEVELOPMENT"

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


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">

<soap:Body xmlns:m="http://www.example.org/stock">
  <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


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: How to access SOAP services from iPhone

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜