Mahesh Babu
Mahesh Babu

Reputation: 3433

how to write xml for soap message

I am new to web services in iphone.

I need to get data from .net server.

I fallow this tutorial

In this soap message is

NSString *soapMessage = [NSString stringWithFormat:
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<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/\">\n"
    "<soap:Body>\n"
    "<Hello xmlns=\"http://viium.com/\">\n"
    "<name>%@</name>\n"
    "</Hello>\n"
    "</soap:Body>\n"
    "</soap:Envelope>\n", nameInput.text
    ];

But i did n't have knowledge on this,

I am from java background.

In android my xml string is

xml="<MortgageGetLoanOfficerInfo><PhoneNumber>"+getDeviceTelNo()+"</PhoneNumber></MortgageGetLoanOfficerInfo>";

how can i write this in iphone.

can any pls post solution.

Thank u in advance.

Upvotes: 0

Views: 2649

Answers (2)

jfalexvijay
jfalexvijay

Reputation: 3711

try with following sample code;

NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<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/\">\n"
                             "<soap:Body>\n"
                             "<getLogin xmlns=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
                             "<email>%@</email>\n"
                             "<user_pwd>%@</user_pwd>\n"
                             "</getLogin>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n", userNameField.text,passWordField.text];
NSLog(@"%@", soapMessage);
NSURL *url = [NSURL URLWithString:@"web service url"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"your soap action" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection ){
    webData = [[NSMutableData data] retain];
}else{
    NSLog(@"theConnection is NULL");
}

Upvotes: 1

Kapil Choubisa
Kapil Choubisa

Reputation: 5232

You are correctly assigning your soap request in NSString. Now you have to create NSMutableURLRequest object and send the request as follow

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:webServiceUrl];

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];              
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:yourSoapAction forHTTPHeaderField:@"SOAPAction"];
[req addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];    
[req setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];  
NSData *webData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error]; 

Now parse your webData using any of xml parser. I generally prefer NSXMLParser

Hope this helped..

Upvotes: 5

Related Questions