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 请求的更多相关文章

  1. Java发布一个简单 webservice应用 并发送SOAP请求

    一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...

  2. Java发布webservice应用并发送SOAP请求调用

    webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...

  3. WebService SOAP、Restful和HTTP(post/get)请求区别

    web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力. Web service 就是一个应用程序,它向外界暴露出一个能够通过We ...

  4. 浅谈WebService SOAP、Restful、HTTP(post/get)请求

    http://www.itnose.net/detail/6189456.html 浅谈WebService SOAP.Restful.HTTP(post/get)请求 2015-01-09 19:2 ...

  5. Jmeter发送SOAP请求对WebService接口测试

    Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe,  application/soap+xml;c ...

  6. Axis2(10):使用soapmonitor模块监视soap请求与响应消息

    在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...

  7. PHP用post来进行Soap请求

    最近调了一个Soap请求C# webservice的项目.网上坑不少. 使用原生的SoapClient库请求也是失败.只好用post来进行模拟了.代码贴出来,给大家参考一下. <?php nam ...

  8. webService(SOAP)性能测试脚本

    本文以天气预报的webService为基础进行学习   webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ...

  9. [转]C#通过Http发送Soap请求

    /// <summary>        /// 发送SOAP请求,并返回响应xml        /// </summary>        /// <param na ...

随机推荐

  1. Python index()方法

    Python index()方法  Python 字符串 描述 Python index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否 ...

  2. 【IntelliJ IDEA】idea或者JetBrains公司所有编辑器,设置其软件的字体样式

    操作如下: 修改完成后的效果: 可以看到修改以后的ide的效果:

  3. python测试开发django-28.发送邮件send_mail

    前言 django发邮件的功能很简单,只需简单的配置即可,发邮件的代码里面已经封装好了,调用send_mail()函数就可以了 实现多个邮件发送可以用send_mass_mail()函数 send_m ...

  4. centos7编译安装nginx及无缝升级https

    安装依赖: yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel 下载nginx: wget -c  ...

  5. Button 在布局文件中定义监听器,文字阴影,自定义图片,代码绘制样式,添加音效的方法

    1.Button自己在xml文件中绑定监听器 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  6. 使用Python读取Mp3的标签信息

    什么是ID3 MP3是音频文件最流行的格式,它的全称是 MPEG layer III.但是这种格式不支持对于音频内容的描述信息,包括歌曲名称.演唱者.专辑等等. 因此在1996年,Eric Kemp在 ...

  7. tf.argmax

    tf.argmax(input, axis=None, name=None, dimension=None) Returns the index with the largest value acro ...

  8. Springmvc的handler method参数绑定常用的注解

    转自:http://blog.longjiazuo.com/archives/1149   1. 简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分 ...

  9. 【BZOJ】【4144】【AMPPZ2014】Petrol

    最短路+最小生成树+倍增 图论问题中综合性较强的一题= =(Orz vfk) 比较容易发现,关键的还是有加油站的这些点,其他点都是打酱油的. 也就是说我们重点是要求出 关键点之间的最短路. 这玩意…… ...

  10. Verilog 加法器和减法器(6)

    为了减小行波进位加法器中进位传播延迟的影响,可以尝试在每一级中快速计算进位,如果能在较短时间完成计算,则可以提高加法器性能. 我们可以进行如下的推导: 设 gi=xi&yi, pi = xi ...