1

私は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」を印刷すると、「<>」という空の応答が返されます。私はIOSが初めてです。私のコードの問題は何ですか(または)他の方法を使用して https で SOAP Web サービスを呼び出し、いくつかの有用なリンクを提供する必要があります。

編集 済み: コードを次のように変更しました。

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