Xcode 发送 HTTPS Soap 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8323023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode send a HTTPS Soap request
提问by Tomzie
Yesterday the HTTPs certificate was installed on my server, where my webservice is running. Before the HTTPs certificate was installed, my self-made iPhone app sent a SOAP request to the webservice. The webservice succesfully answered by returning an XML message.
昨天,HTTPS 证书安装在我的服务器上,我的网络服务正在运行。在安装 HTTPS 证书之前,我自制的 iPhone 应用程序向网络服务发送了一个 SOAP 请求。Web 服务通过返回 XML 消息成功回答。
This is the code which I use for sending the soap request to the webservice:
这是我用于将soap请求发送到网络服务的代码:
NSString *soapMsg = [NSString stringWithFormat:@""
""
""
""
"%@"
"%@"
""
""
"", parameter1, parameter2];
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]);
Now if I change the URL to "httpS://domain.com/webservice-name.svc", the webservice doesn't repsonse at all.
现在,如果我将 URL 更改为“httpS://domain.com/webservice-name.svc”,则 Web 服务根本不会响应。
What do I have to change to send a succesfull SOAP request to a HTTPS secured webservice?
要向 HTTPS 安全 Web 服务发送成功的 SOAP 请求,我需要更改什么?
采纳答案by Jobin Jose
For normal xml service using GET method https request was working for me. Try this delegate methods that I have used.`
对于使用 GET 方法 https 请求的普通 xml 服务对我有用。试试我用过的这个委托方法。`
- (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];
}