Reputation: 1719
I am developing an IOS application. In which I want to call a soap webservice method(transport based security (HTTPS)). I am following NSURLConnection to call the web service method. I have used the following code.
- (void)viewDidLoad
{
//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<SOAP-ENV:Envelope \n"
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
"xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
"<SOAP-ENV:Body> \n"
"<Service xmlns=\"http://tempuri.org/\">"
"</Service> \n"
"</SOAP-ENV:Body> \n"
"</SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:@"https://domain/Service.svc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/IService/RegisterMethod" 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] ;
}
else {
NSLog(@"theConnection is NULL");
}
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSArray *trustedHosts=[NSArray arrayWithObject:@"https://mydomain/"];
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
But I can't get the response. When I print the "webData", I am getting "<>" empty response. I am new to the IOS. Can anyone tell me, what is the problem in my code (or) I should use any other method to call soap web service with https and give me some useful links.
Edited: I have changed my code as,
NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];
[theRequest addValue: [NSString stringWithFormat:@"%d", bodyDataLength] forHTTPHeaderField:@"Content-Length"]
[theRequest setHTTPBody: bodyData]
But still I am getting the "webData" value as empty.
Upvotes: 1
Views: 4944
Reputation: 19106
It looks almost good ;)
You set the body data as a NSData
object which you obtain by converting the soap message string into a byte sequence using the character encoding specified in the charset parameter of the Content-Type header (UTF-8).
So far so good. But, the length of the body data is the length of the NSData
content (in bytes) - not the length of the NSString
object in UTF-16 code units. ;)
NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];
Set the Content-Length header accordingly.
Upvotes: 2