将参数发送到 Web 服务

2023-12-31

开始之前:我正在使用 Objective 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];

我也实现了这些方法

  • 收到响应
  • didRecieveAuthenticationChallenge
  • 已收到数据
  • 因错误而失败
  • 连接完成加载。

而且效果很好。

Now我需要向函数发送 2 个参数:“位置”和“模块”。
我尝试使用以下修改:

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

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

额外信息:
我无权访问网络服务的源代码,因此无法修改它。 但我可以访问 WSDL。制作该函数的人都在那里...但我无法理解它>.<...>

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


我花了几个小时才完成这项工作,所以我想我应该发布我的解决方案,这样可以在将来节省其他人的时间。这就是我如何让 Objective-c 使用字符串参数从 .NET WCF 服务检索 JSON 数据。

(我使用wireshark捕获与wcf服务通信的.net客户端,这样我就知道需要从我的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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将参数发送到 Web 服务 的相关文章

随机推荐