iOS webservice SOAP 请求
1. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
简单来说 webservice 请求就是是往服务器POST XML数据;
然后服务器响应得到的也是XML数据;
2. 如下使用iOS NSUrlConnection 接口进行webservice 请求示例;
测试使用的接口 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx
根据接口描述使用了两种版本的soap请求:soap1.1,soap1.2 两种区别在于 soap请求体xml的格式上;
实现原理其实根据接口上的soap体描述组合xml包;
//SOAP 1.1
- (void)soapv11Request
{
NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
req.HTTPMethod = @"POST";
[req setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"" forKey:@""];
[req setValue:@"http://WebXml.com.cn/getMobileCodeInfo" forHTTPHeaderField:@"SOAPAction"]; NSString *reqBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
<soap:Body>\
<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\
<mobileCode>%@</mobileCode>\
<userID></userID>\
</getMobileCodeInfo>\
</soap:Body>\
</soap:Envelope>",self.phoneNumTF.text]; NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
req.HTTPBody = reqData; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"Error:%@",connectionError);
}
else
{
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
} }];
}
//SOAP 1.2
- (void)soapv12Request
{
NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
req.HTTPMethod = @"POST";
[req setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; NSString *reqBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<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>\
<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\
<mobileCode>%@</mobileCode>\
<userID></userID>\
</getMobileCodeInfo>\
</soap12:Body>\
</soap12:Envelope>",self.phoneNumTF.text]; NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
req.HTTPBody = reqData; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"Error:%@",connectionError);
}
else
{
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
} }];
}
示例工程下载:https://github.com/cocoajin/TDDDemo/tree/master/WebServiceTest
3. 在实际应用中,我们可以使用一些xml处理的相关第三方类库来组合soap体;
组合好soap请求体之后,往指定接口POST 请求数据即可;
也有专门处理SOAP请求的类库:https://github.com/priore/SOAPEngine 可以了解一下
iOS webservice SOAP 请求的更多相关文章
- Java发布一个简单 webservice应用 并发送SOAP请求
一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...
- Java发布webservice应用并发送SOAP请求调用
webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...
- WebService SOAP、Restful和HTTP(post/get)请求区别
web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力. Web service 就是一个应用程序,它向外界暴露出一个能够通过We ...
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
http://www.itnose.net/detail/6189456.html 浅谈WebService SOAP.Restful.HTTP(post/get)请求 2015-01-09 19:2 ...
- Jmeter发送SOAP请求对WebService接口测试
Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe, application/soap+xml;c ...
- Axis2(10):使用soapmonitor模块监视soap请求与响应消息
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...
- PHP用post来进行Soap请求
最近调了一个Soap请求C# webservice的项目.网上坑不少. 使用原生的SoapClient库请求也是失败.只好用post来进行模拟了.代码贴出来,给大家参考一下. <?php nam ...
- webService(SOAP)性能测试脚本
本文以天气预报的webService为基础进行学习 webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ...
- [转]C#通过Http发送Soap请求
/// <summary> /// 发送SOAP请求,并返回响应xml /// </summary> /// <param na ...
随机推荐
- eclipse使用profile完成不同环境的maven打包功能
原文:https://blog.csdn.net/duan9421/article/details/79086335 我们在日常开发工作中通常会根据不同的项目运行环境,添加不同的配置文件,例如 开发环 ...
- Linux学习17-gitlab访问慢502问题优化
前言 浏览器访问gitlab的web页面,发现非常慢,并且很容易出现502问题.其中一个原因就是8080端口被tomcat占用,前面一篇已经更换了端口,但还是很慢. 后来搜了下,原因是gitlab占用 ...
- window.opener()方法
<!DOCTYPE html><html><head><meta charset="GBK"><title>菜鸟教程(r ...
- 浅谈Java Future接口
Java项目编程中,为了充分利用计算机CPU资源,一般开启多个线程来执行异步任务.但不管是继承Thread类还是实现Runnable接口,都无法获取任务执行的结果.JDK 5中引入了Callable和 ...
- 使用 Golang 编写链代码 (v0.6 )
https://www.ibm.com/developerworks/cn/cloud/library/cl-ibm-blockchain-chaincode-testing-using-golang ...
- 节约内存,请使用标签页管理工具:onetab、better onetab
OneTab可以管理chrome和firefox的标签页,把暂时不用的标签页收藏起来,形成一个列表,当然,可以对列表进行分类管理,以方便后续打开查看. 这样就不用打开很多tab,占用大量内存. 由于O ...
- python的单例模式:
python的单例模式:http://funhacks.net/2017/01/17/singleton/ https://www.cnblogs.com/huchong/p/8244279.html ...
- Maven中的库(repository)详解
Maven中的库(repository)是构件(artifact)的集合.构件以一定的布局存储在库中. 本地仓库 vs. 远程仓库 运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获 ...
- libxml2 使用教程【转】
https://blog.csdn.net/zhoudaxia/article/details/8565731# 本文整理自官方使用教程http://xmlsoft.org/tutorial/inde ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...