问题描述
我想从iOS应用程序调用.NET ASMX服务。我创建了这样的SOAP消息:
I want to invoke the .NET ASMX service from iOS application. I created my SOAP messages like this:
-(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];
}
以上代码有效但你可以看我必须手动创建SOAP消息。有没有办法只传递参数和方法名称,并自动创建肥皂体/消息。 StackOverFlow上有一个相同的场景示例,但我无法找到它。
The above code works but as you can see I have to manually create the SOAP message. Is there any way to just pass in the parameters and the method name and the soap body/message is created automatically. There was an example on StackOverFlow for the same exact scenario but I am not able to find it.
推荐答案
假设您可以为您的服务生成WSDL,我建议您查看适用于iOS的WSDL2SOAP。我在一个历史项目中使用过这个(2年多以前)并且效果很好: -
Assuming that you can generate a WSDL for your services I recommend that you look at WSDL2SOAP for iOS. I have used this in a historical project (2+ years ago) and it worked well:-
这是你的应用指向WSDL,它会自动生成用于访问SOAP服务的类。我不能保证它仍然可以工作,但我建议调查,因为它使SOAP处理变得容易。请记住,库(除非最近更新)是ARC之前的,因此除了文档中的安装步骤之外,您还需要将其标记为适当的内存管理。
This is an app that you point to a WSDL and it automatically generates classes for you to use to access SOAP services. I can't vouch that it still works, but I'd recommend investigating because it makes for easy SOAP handling. Do remember that the library (unless recently updated) is pre-ARC so in addition to the installation steps in the docs, you will need to flag this for appropriate memory management.
这篇关于从iOS应用程序向SOAP ASMX服务发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!