我想从iOS应用程序调用.NET ASMX服务.我创建了这样的SOAP消息:
-(IBAction)submitButtonClicked:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/NotificationService.asmx"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[request addRequestHeader:@"SOAPAction" value:@"http://tempuri.org/HelloWorld"];
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<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/\">"
"<soap:Body>"
"<Greet xmlns=\"http://tempuri.org/\">"
"<deviceToken>some device token</deviceToken>"
"<userName>azamsharp</userName>"
"</Greet>"
"</soap:Body>"
"</soap:Envelope>"];
NSString *messageLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
[request addRequestHeader:@"Content-Length" value:messageLength];
[request appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
[request startAsynchronous];
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以正常工作,但正如您所看到的,我必须手动创建SOAP消息.有没有办法只传入参数和方法名称,并自动创建soap正文/消息.StackOverFlow上有一个示例用于相同的场景,但我无法找到它.
假设您可以为您的服务生成 WSDL,我建议您查看适用于 iOS 的 WSDL2SOAP。我在一个历史项目中使用过这个(两年多前)并且效果很好:-
http://code.google.com/p/wsdl2objc/
这是一个指向 WSDL 的应用程序,它会自动生成类供您用来访问 SOAP 服务。我不能保证它仍然有效,但我建议您进行研究,因为它可以轻松处理 SOAP。请记住,该库(除非最近更新)是 ARC 之前的,因此除了文档中的安装步骤之外,您还需要对其进行标记以进行适当的内存管理。
| 归档时间: |
|
| 查看次数: |
6553 次 |
| 最近记录: |