2

ASIHttpRequest を使用して SOAP Web サービスを使用するアプリケーションがあり、他のネットワーク フレームワークを使用したい (または使用したい) ので、AFNetworking を選択しましたが、SOAP を使用する方法がわかりません。

NSString *operation=[NSString stringWithString:@"search_service"];
NSString *xmlNamespace=[NSString stringWithString:@"http://www.xxx.com/wsdl"];
NSString *address=[NSString stringWithString:@"http://www.xxx.com/service"];
NSString *parameters=[NSString stringWithFormat:@"<param1>%@</param1><param2>%@</param2>",
                      @"val1",
                      @"val2",
                      ];

NSString *operatorTag = [NSString stringWithFormat:@"<%@ xmlns=\"%@\">%@</%@>\n", operation, xmlNamespace, parameters, operation];

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n"
                         "  <s:Header>\n"
                         "    <To xmlns=\"http://www.w3.org/2005/08/addressing\">%@</To>\n"
                         "    <a:Action>http://tempuri.org/IService1/%@</a:Action>\n"
                         "  </s:Header>\n"
                         "  <s:Body>\n"
                         "    %@"
                         "  </s:Body>\n"
                         "</s:Envelope>\n", address, operation, operatorTag
                         ];

asiRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:address]];
[asiRequest setDelegate:self];
[asiRequest addRequestHeader:@"application/soap+xml; charset=utf-8" value:@"Content-Type"];
[asiRequest setRequestMethod:@"POST"];
[asiRequest setPostBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[asiRequest startAsynchronous];

編集:これは私がAFNetworkingで試したことです:

NSString *operationWSDL= @"search_service";
NSString *xmlNamespace= @"http://www.xxx.com/wsdl";
NSString *address= @"http://www.xxx.com/service";
NSString *parameters=[NSString stringWithFormat:@"<param1>%@</param1><param2>%@</param2>",@"val1",@"val2",];


NSString *operatorTag = [NSString stringWithFormat:@"<%@ xmlns=\"%@\">%@</%@>\n", operationWSDL, xmlNamespace, parameters, operationWSDL];

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\n"
                         "  <s:Header>\n"
                         "    <To xmlns=\"http://www.w3.org/2005/08/addressing\">%@</To>\n"
                         "    <a:Action>http://tempuri.org/IService1/%@</a:Action>\n"
                         "  </s:Header>\n"
                         "  <s:Body>\n"
                         "    %@"
                         "  </s:Body>\n"
                         "</s:Envelope>\n", address, operationWSDL, operatorTag
                         ];

NSURL *url = [NSURL URLWithString:@"http://www.xxx.com/service"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/soap+xml; charset=utf-8",@"Content-Type", nil] body:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
4

1 に答える 1

0

WSDL2Objc を試しましたか? http://code.google.com/p/wsdl2objc/ . 最近、プロジェクトで使用しました。SOAP サービスで動作するクラスを (消費された WSDL から) 正常に生成します。より良い解決策になると思います。

于 2012-09-13T13:55:57.907 に答える