Xcode发送HTTPS Soap请求

时间:2011-11-30 08:30:10

标签: iphone xcode soap https request

昨天我的服务器上安装了HTTPs证书,我的webservice正在运行。在安装HTTPs证书之前,我自制的iPhone应用程序向Web服务发送了SOAP请求。通过返回XML消息成功回答了Web服务。

这是我用于将soap请求发送到webservice的代码:

NSString *soapMsg = [NSString stringWithFormat:@"" "" "" "" "%@" "%@" "" "" "", parameter1, parameter2];

NSURL *url = [NSURL URLWithString:
              @"http://domain.com/webservice-name.svc"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = [NSString stringWithFormat:@"%d",
                       [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/webservice-name/method-name" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *resultsData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

if(error){
    NSLog(@"Error sending soap request: %@", error);
    return FALSE;
}

NSLog(@"%@", soapMsg);
NSLog(@"%@", [[NSString alloc] initWithData:resultsData encoding:NSUTF8StringEncoding]);

NSURL *url = [NSURL URLWithString: @"http://domain.com/webservice-name.svc"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:@"http://tempuri.org/webservice-name/method-name" forHTTPHeaderField:@"SOAPAction"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; [req setHTTPMethod:@"POST"]; [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; NSURLResponse *response = nil; NSError *error = nil; NSData *resultsData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; if(error){ NSLog(@"Error sending soap request: %@", error); return FALSE; } NSLog(@"%@", soapMsg); NSLog(@"%@", [[NSString alloc] initWithData:resultsData encoding:NSUTF8StringEncoding]);

现在,如果我将URL更改为“httpS://domain.com/webservice-name.svc”,则webservice根本不会回复。

我需要更改以将成功的SOAP请求发送到HTTPS安全的Web服务吗?

1 个答案:

答案 0 :(得分:2)

对于使用GET方法的正常xml服务,https请求对我有用。试试我使用过的委托方法。


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSArray *trustedHosts=[NSArray arrayWithObject:@"yourdomian.com"];
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];
}