将参数发送到Web服务

时间:2010-04-05 20:35:08

标签: iphone objective-c web-services parameters

在开始之前:我正在使用目标C为Iphone编程。

我已经使用NSURLRequest和NSURLConnection实现了对Web服务功能的调用。然后该函数返回一个带有我需要的信息的XML。

代码如下:

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

我也实现了方法

  • didRecieveResponse
  • didRecieveAuthenticationChallenge
  • didRecievedData
  • didFailWithError
  • connectionDidFinishLoading。

它完美无缺。

现在我需要向函数发送2个参数:“location”和“module”。
我尝试使用以下修改:

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

但它似乎不起作用。 我做错了什么?有没有办法知道我是否使用错误的参数名称(因为它可能是“位置”或“位置”或无关紧要?)? 或者知道哪些参数是等待的函数......

额外信息:
我无权访问Web服务的来源,因此无法对其进行修改。 但我可以访问WSDL。制作这个功能的人说的都是......但是我对它没有任何意义>。< ...

任何帮助将不胜感激。 :)

4 个答案:

答案 0 :(得分:3)

花了我几个小时来完成这项工作,所以我想我会发布我的解决方案,以便将来节省其他人一些时间。这就是我如何通过字符串参数从.NET WCF服务中检索JSON数据的目标-c。

(我使用wireshark捕获.net客户端与wcf服务通话,因此我知道我需要从iOS客户端传递的内容)

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

答案 1 :(得分:1)

您在HTTP标头中设置值,而不是HTTP请求中的GET或POST参数。这可能不是你想要做的。

如果服务器接受GET请求中的参数,您可以在URL中执行以下操作:

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

如果不这样做,你将不得不像MK所说的那样去完整的SOAP路线。

答案 2 :(得分:0)

如果涉及WSDL,它可能是一个SOAP Web服务,所以它可能需要一些SOAPy XML作为输入,我不认为你这样做。或许看一下这个问题:How to access SOAP services from iPhone

答案 3 :(得分:0)

关于GET Post SOAP的一些例子

获取请求

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

发布请求

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

肥皂请求

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>

主机,用户代理,内容长度,内容类型是请求标头中的项目

相关问题