【问题标题】:AFNetworking + SOAP web serviceAFNetworking + SOAP 网络服务
【发布时间】:2012-09-06 14:45:45
【问题描述】:

我有一个使用 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];

【问题讨论】:

  • What have you tried 没用?我们在这里提供帮助,但不是在这里为您编写代码。
  • 是的,也许我的问题让你这么想。我用我尝试做的事情来编辑我的问题。

标签: iphone ios web-services soap afnetworking


【解决方案1】:

你试过 WSDL2Objc 吗? http://code.google.com/p/wsdl2objc/。 我最近在我的项目中使用了它。它成功地生成类(从消费的 WSDL)以使用 SOAP 服务。我认为这将是更好的解决方案。

【讨论】:

  • 谢谢,我之前尝试过(在其他项目 Web 服务中),它工作得很好,但是使用这个返回值是空的,我无法更改服务器中的任何内容,所以我中止了这个解决方案.
  • 警告。我尝试了 WSDL2Objc,但收效甚微。 ARC 支持卡在非发布分支中。我使用的几个 WSDL 在生成代码时会失败。上一次发布几乎是 3 年前。
  • 尝试使用 SudzC 生成 Objective-C ARC 和非 ARC WSDL 自动代码。我发现它确实很好用。 sudzc.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多