IOS-使用基于传输的安全性(HTTPS)调用soap Web服务

IOS-使用基于传输的安全性(HTTPS)调用soap Web服务,ios,web-services,soap,https,nsurlconnection,Ios,Web Services,Soap,Https,Nsurlconnection,我正在开发一个IOS应用程序。我想在其中调用soap Web服务方法(基于传输的安全性(HTTPS))。我按照NSURLConnection调用web服务方法。 我使用了以下代码 - (void)viewDidLoad { //Web Service Call NSString *soapMessage = [NSString stringWithFormat: @"<?xml version=\"1.0\" en

我正在开发一个IOS应用程序。我想在其中调用soap Web服务方法(基于传输的安全性(HTTPS))。我按照NSURLConnection调用web服务方法。 我使用了以下代码

- (void)viewDidLoad
{

    //Web Service Call
    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                             "<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/\"> \n"
                             "<SOAP-ENV:Body> \n"
                             "<Service xmlns=\"http://tempuri.org/\">"
                             "</Service> \n"
                             "</SOAP-ENV:Body> \n"
                             "</SOAP-ENV:Envelope>"];



    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];
}
但我仍然得到“webData”值为空

看起来差不多不错;)

将主体数据设置为
NSData
对象,该对象是通过使用内容类型头(UTF-8)的charset参数中指定的字符编码将soap消息字符串转换为字节序列而获得的

到目前为止还不错。但是,正文数据的长度是
NSData
内容的长度(字节)-而不是
NSString
对象的长度(UTF-16代码单位)


相应地设置内容长度标题。

感谢您的回复。我已经更改了问题中编辑部分中的代码。但我仍然将“webData”值设置为空。我将方法名称设置为[theRequest addValue:@'(HttpHeaderField:@“soapAction”);这是正确的吗?从技术上讲,这个要求对我来说似乎还可以。但是,身份验证处理忽略了信任评估,因此不能安全地处理服务器信任验证!此外,我无法判断“soapAction”是否正确(请参考web服务API)。此外,我假设您实现了所有必需的委托方法—例如,
连接:didReceiveResponse:
连接:didReceiveData
连接:didFailWithError:
?请检查所有错误,记录错误消息,并慷慨地在代码中添加断言。我已经检查了didReceiveResponse,得到了415个错误代码。问题是“不支持的媒体类型”请求。因此,我将我的内容类型更改为“application/soap+xml”。我还咨询了web服务API,并对我的请求进行了更改,如“SOAP-ENV:”到“s:”(即)到。现在我得到了正确的回答。感谢CouchDeveloper。很高兴听到您解决了这些问题:)现在您可能需要处理服务器信任验证:如果服务器的证书由知名CA签名,您可能不需要覆盖身份验证委托:
NSURLConnection
的默认行为很好。否则,对于自签名证书,您必须实现相应的身份验证委托并手动评估信任。你应该收集更多关于这个主题的信息,例如从苹果公司的文档等。
NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];
[theRequest addValue: [NSString stringWithFormat:@"%d", bodyDataLength] forHTTPHeaderField:@"Content-Length"]
[theRequest setHTTPBody: bodyData]
NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];