簡體   English   中英

AFNetworking + SOAP 網絡服務

[英]AFNetworking + SOAP web service

我有一個使用 ASIHttpRequest 使用 SOAP Web 服務的應用程序,現在我想(或已經)使用其他網絡框架,我選擇了 AFNetworking,但看不到我如何處理 SOAP 使用,她是我如何處理 ASIHttpRequest :

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];

你試過 WSDL2Objc 嗎? http://code.google.com/p/wsdl2objc/ 我最近在我的項目中使用了它。 它成功地生成了類(從使用的 WSDL)來處理 SOAP 服務。 我認為這將是更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM