title: IOS - calling soap webservice with transport based security (HTTPS)
body: <p>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.</p>
  
  <pre><code>- (void)viewDidLoad
  {
  
      //Web Service Call
      NSString *soapMessage = [NSString stringWithFormat:
                               @"&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"
                               "&lt;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/\"&gt; \n"
                               "&lt;SOAP-ENV:Body&gt; \n"
                               "&lt;Service xmlns=\"http://tempuri.org/\"&gt;"
                               "&lt;/Service&gt; \n"
                               "&lt;/SOAP-ENV:Body&gt; \n"
                               "&lt;/SOAP-ENV:Envelope&gt;"];
  
  
  
      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];
  }
  </code></pre>
  
  <p>But I can't get the response. When I print the "webData", I am getting "&lt;>" 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.</p>
  
  <p><strong>Edited:</strong>
  I have changed my code as,</p>
  
  <pre><code>NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
  NSUInteger bodyDataLength = [bodyData length];
  [theRequest addValue: [NSString stringWithFormat:@"%d", bodyDataLength] forHTTPHeaderField:@"Content-Length"]
  [theRequest setHTTPBody: bodyData]
  </code></pre>
  
  <p>But still I am getting the "webData" value as empty. </p>