本文章采用的字符串常量:
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
相关推荐
以上就是Retrofit+Soap对webservice进行访问请求具体实现,该实现是通过String字符串的拼接,传输过程中转化为xml数据格式来实现的。
WebService教程+实例+代码提示功能WebService实例.rar
实战Web+Service+with+CXF webservice快速入门 webservice快速入门
在eclipse下用tomcat服务器和axis插件创建WebService服务,doc文档
NULL 博文链接:https://ajita.iteye.com/blog/1745777
Spring+Hibernate+Xfire+发布WebService教程文档
java项目简单模拟soap型的webservice远程调用服务,内附使用说明文档方便学习和查阅,让你轻松学会soap-webservice的使用方法!
JSP+Servlet+SqlServer+WebService(XFire)E-家园网站 非常适合自学使用
这里少了一个类,是根据实体类生成xml的文件下载地址为:http://download.csdn.net/detail/qq_14996421/9495688
eclipse+tomcat+axis2创建webservice
Weblogic92+CXF+XDoclet+Spring轻松搞定webservice 例子
cxf+spring+jdk1.5开发webService部署weblogic9.2
Struts2实现国际化+天气webservice接口+股票webservice接口,源代码可直接使用,用MyEclipse直接导入
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技术传送文件、大规模数据访问时...
Struts+Spring+Hibernate+WebService集成架构
CXF+Spring+自定义拦截器 webservice源码下载
使用Simple Frontend+Aegis方式发布并获取webservice.doc
利用webService实现的新闻系统 实现了新闻的添加、编辑、删除以及用户管理功能,功能上比较简单,但是都是通过调用webService来实现的,而且采用了MVC面向对象模式进行开发,用户登录采用了自定义控件 安装注意: ...
Spring+CXF+tomcat开发webservice,包含服务端和客户端,导入myeclipse就可以用。帮助大家搭建webservice框架,实现webservice功能。