1

我正在开发一个 IOS 应用程序。我想在其中调用一个肥皂网络服务方法(基于传输的安全性(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”时,我得到“<>”空响应。我是IOS的新手。谁能告诉我,我的代码有什么问题(或)我应该使用任何其他方法通过 https 调用肥皂网络服务并给我一些有用的链接。

编辑: 我已将代码更改为,

NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];
[theRequest addValue: [NSString stringWithFormat:@"%d", bodyDataLength] forHTTPHeaderField:@"Content-Length"]
[theRequest setHTTPBody: bodyData]

但我仍然将“webData”值设为空。

4

1 回答 1

2

它看起来几乎不错;)

您可以将正文数据设置为一个NSData对象,该对象是通过使用 Content-Type 标头 (UTF-8) 的 charset 参数中指定的字符编码将 soap 消息字符串转换为字节序列而获得的。

到目前为止,一切都很好。但是,正文数据的长度是NSData内容的长度(以字节为单位),而不是NSStringUTF-16 代码单元中的对象长度。;)

NSData* bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger bodyDataLength = [bodyData length];

相应地设置 Content-Length 标头。

于 2013-09-17T15:45:56.347 回答