2011-11-30 259 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]; 
}