本文章采用的字符串常量:
NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getSupportCity xmlns=\"http://WebXml.com.cn/\">" "<byProvinceName>ALL</byProvinceName>" "</getSupportCity>" "</soap12:Body>" "</soap12:Envelope>"; NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
之前写的代码有问题,因为webxml这个网站支持GET、POST和SOAP协议,所以在接收到回执的时候误以为是SOAP请求成功,但仔细看了一下返回信息发现是普通的POST请求成功的消息。以下是错误的代码
AFNetworking 2.0
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]]; manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init]; [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:@"WebServices/WeatherWebService.asmx/getSupportCity" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded", @"Content-Type", soapLength, @"Content-Length", nil] body:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; }]; [manager POST:@"/WebServices/WeatherWebService.asmx/getSupportCity" parameters:@{@"byProvinceName":@"ALL"} success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding]; NSLog(@"responseObject: %@", response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
在发现错误后,本人调试跟踪了一下AFNetworking2.0的POST过程,发现在添加Content-Type和Content-Length的时候,AFNetworking把字段值改掉了,所以在AFHTTPRequestOperationManager类中添加了如下方法:
- (AFHTTPRequestOperation *)SOAP:(NSString *)URLString constructingBodyWithBlock:(void (^)(NSMutableURLRequest *request))block success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:nil constructingBodyWithBlock:nil]; block(request); AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; [self.operationQueue addOperation:operation]; return operation; }
添加好此方法后,直接调用就可以了
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.webxml.com.cn"]]; manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init]; [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"]; [manager SOAP:@"/WebServices/WeatherWebService.asmx" constructingBodyWithBlock:^(NSMutableURLRequest *request) { [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@, %@", operation, response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding]; NSLog(@"%@, %@", operation, error); }];
AFNetworking2.0 + SOAP调用成功!!!
闲着没事把之前的代码拿来又看了一遍,想着能不能在不改动AFNetworking代码的前提下调用SOAP协议成功,如果有同样想法的朋友可以试一试下面的代码
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init]; [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"]; NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" parameters:nil]; [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@, %@", operation, response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding]; NSLog(@"%@, %@", operation, error); }]; [manager.operationQueue addOperation:operation];}
调用成功!
ASIHTTPRequest NSString *soapMessage = @"<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getSupportCity xmlns=\"http://WebXml.com.cn/\">" "<byProvinceName>ALL</byProvinceName>" "</getSupportCity>" "</soap12:Body>" "</soap12:Envelope>"; NSString *soapLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; NSURL *url = [NSURL URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request addRequestHeader:@"Content-Type" value:@"application/soap+xml; charset=utf-8"]; [request addRequestHeader:@"Content-Length" value:soapLength]; [request setRequestMethod:@"POST"]; [request appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; [request setDefaultResponseEncoding:NSUTF8StringEncoding]; [request startAsynchronous];
参考资料
http://blog.csdn.net/iamstillzhang/article/details/8264049
相关推荐
本篇文章将详细探讨如何使用Retrofit2、OkHttp3和RxJava这三个强大的库来构建一个Android客户端,以实现对SOAP WebService的调用。 首先,我们来看Retrofit2,这是一个由Square公司开发的类型安全的HTTP客户端。...
在本讨论中,我们将深入探讨如何利用C# 2.0的TraceExtension来记录WebService的SOAP日志。这对于调试、性能分析以及理解服务间的交互至关重要。 SOAP(简单对象访问协议)是Web服务的标准通信协议,用于交换结构化...
在本文中,我们将深入探讨如何在 Eclipse 集成开发环境中,利用 Tomcat 服务器和 Axis 插件创建并发布一个 WebService。这是一个详细步骤的指南,涵盖了从环境准备到测试 WebService 的全过程。 首先,我们需要确保...
实战Web+Service+with+CXF webservice快速入门 webservice快速入门
通过阅读提供的"java-soap-webservice"文档,你可以进一步了解具体的实现步骤,包括如何设置项目、配置JAX-WS、生成客户端代码、编写调用服务的代码,以及如何解析响应。实践中,不断动手操作和调试是掌握这一技术的...
Spring+Hibernate+Xfire+发布WebService教程文档
学习WebService,你需要了解SOAP(Simple Object Access Protocol)、WSDL(Web Services Description Language)和UDDI(Universal Description, Discovery and Integration)这三个核心概念。SOAP定义了消息格式,...
这里少了一个类,是根据实体类生成xml的文件下载地址为:http://download.csdn.net/detail/qq_14996421/9495688
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
线程的使用、*NET 2.0 新特性、webservice 文件监视、C#高级应用(如何将聊天程序做成windows服务、COM+服务解决同时访问大量数据并发性、COM+服务实现银行转账系统、如何用Remoting技术传送文件、大规模数据访问时...
CXF+Spring+自定义拦截器 webservice源码下载
总结,Android应用基于Retrofit2+Okhttp3通过SOAP协议请求WebService是一种有效的解决方案,它简化了网络调用的复杂性,同时利用了OkHttp3的高性能特性。通过定义接口、配置Retrofit实例、创建SOAP请求和处理响应,...
利用webService实现的新闻系统 实现了新闻的添加、编辑、删除以及用户管理功能,功能上比较简单,但是都是通过调用webService来实现的,而且采用了MVC面向对象模式进行开发,用户登录采用了自定义控件 安装注意: ...
在Android开发中,与远程服务器进行数据交互是常见的需求,其中SOAP(Simple Object Access Protocol)协议是一种常用的Web Service通信标准,尤其在企业级应用中广泛使用。本教程将重点讲解如何利用Retrofit2和...
它利用XML(eXtensible Markup Language)作为数据交换格式,SOAP(Simple Object Access Protocol)作为消息协议,WSDL(Web Services Description Language)描述服务接口,UDDI(Universal Description, ...
本demo包含 ios远程访问mysql数据库 swift+php+mysql 访问数据库示例 包含详细的搭建方法,包含 cordova +ios 远程访问数据库。webservice方式访问远程数据库。
- 例如,可以将 `geronimo-ws-metadata_2.0_spec-1.1.3.jar` 添加到系统变量中。 #### 四、解决部署问题 - 当部署到 WebLogic 9.2 时,可能会遇到启动异常。 - 可以通过在实现类中设置系统变量,或者在 WebLogic ...
WebService是一种基于标准协议(如SOAP、WSDL和UDDI)的跨平台、跨语言的通信方式,使得不同系统间可以共享服务。XFire是Apache CXF的前身,是一个轻量级的Java WebService框架。它允许开发者轻松创建和消费Web服务...
这是个采用apache cxf实现webservice的示例,主要由spring整合hibernate、struts2实现webservice,里面struts2用到得较少……对于学习很有帮助,另外我觉得若是初学者,不过采用那方式,现在按照套方案做熟练后再去...
**CXF** 是一个开源的Web服务框架,它支持SOAP、RESTful等多种Web服务协议。CXF允许开发者以Java API for Web Services (JAX-WS)和Java API for RESTful Web Services (JAX-RS)的方式来创建和消费Web服务,使得开发...