2011-11-30 144 views
3

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

这是我用来发送SOAP请求发送到web服务的代码:

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]); 

现在,如果我更改URL为“httpS://domain.com/webservice-name.svc “,网络服务根本不反弹。

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

+0

此委托方法是脚本您正在访问实际上呢?例如您是否尝试使用桌面浏览器访问它? (某些Web服务器被配置为在通过SSL加密的HTTP访问时使用完全不同的(root)文件夹)。 – Till

回答

2

对于使用GET方法的普通xml服务,https请求正在为我工​​作。试试,我used.`


- (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]; 
}