开发者

Sending Parameters to the SOAP ASMX Service from iOS Application

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:
                             @开发者_Go百科"<?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]; 


}

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.


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

http://code.google.com/p/wsdl2objc/

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜