WebService数据示例
通过webservice提交xml数据以及soap协议的使用
上次已经给大家分享了简单的webservice的使用,提交给服务器的数据只是简单类型的数据,这次呢换成了xml,并通过一个小例子来讲解soap协议的使用。废话就不多说了先来说下xml数据的上传 1.代码的结构没有多大的变化,只需修改一下请求头就可以了,代码如下
//封装数据,数据以byte方式传输
byte[] b=xml.getBytes();
//需要请求的连接
URL url=new URL("http://192.168.87.1:8080/LoginService/LoginWebService");
//打开连接
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//设置请求方式和消息头以及超时时间
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
//设置是否允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
//取得输出流
OutputStream outPut=conn.getOutputStream();
outPut.write(b);
outPut.flush();
outPut.close();
//判断请求是否成功
if(conn.getResponseCode()==200){
// Log.i();
return "OK";
}else{
return "Err";
}
下面给大家写了点服务器端获取byte数据的代码,顺便在控制台打印xml,这个方法建议写成工具类,我这里就不这样做了
request.setCharacterEncoding("UTF-8");
InputStream in=request.getInputStream();
byte[] b=new byte[1024];
int len=0;
int temp=0;
while((temp=in.read())!=-1){
b[len]=(byte)temp;
len++;
}
System.out.println(new String(b,"UTF-8"));
下面是运行效果图
后面的工作就是解析xml类似的工作了
2.HTTP协议的webservice差不多了下面就是使用soap协议请求webservice了,这里就来弄个手机号码归属地的webservice吧 先打开这个链接http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo 看一下下面的soap协议这里就是用soap1.2就可以 3.下面就根据协议的说明来换掉请求头,为了开发方便我们需要把用于请求的xml写在一个文件里,手机号码用个占位符代替
<?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>$number</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>
4.下面的工作就是替换占位符,请求该webservice
public String readSoapFile(InputStream input,String number) throws Exception{
byte[] b=new byte[1024];
int len=0;
int temp=0;
while((temp=input.read())!=-1){
b[len]=(byte)temp;
len++;
}
String soapxml=new String(b);
//替换xml文件的占位符
Map<String,String> map=new HashMap<String, String>();
map.put("number", number);
return replace(soapxml, map);
}
public String replace(String param,Map<String,String> params) throws Exception{
//拼凑占位符使用正则表达式替换之
String result=param;
if(params!=null&&!params.isEmpty()){
//拼凑占位符
for(Map.Entry<String,String> entry:params.entrySet()){
String name="\\$"+entry.getKey();
Pattern p=Pattern.compile(name);
Matcher m=p.matcher(result);
if(m.find()){
result=m.replaceAll(entry.getValue());
}
}
}
return result;
}
开始请求webservice并得到返回的xml字符串
public String soapService(InputStream input,String number) throws Exception{
String str="";
String soap=readSoapFile(input,number);
//封装数据,数据以byte方式传输
byte[] b=soap.getBytes();
//需要请求的连接
URL url=new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//打开连接
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//设置请求方式和消息头以及超时时间
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
//设置是否允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
//取得输出流
OutputStream outPut=conn.getOutputStream();
outPut.write(b);
outPut.flush();
outPut.close();
//判断请求是否成功
if(conn.getResponseCode()==200){
InputStream in=conn.getInputStream();
byte[] b1=new byte[1024];
int len=0;
int temp=0;
while((temp=in.read())!=-1){
b1[len]=(byte)temp;
len++;
}
str=new String(b1,"UTF-8"); }
return str; }
下面来看下断点运行后的结果
我们能够看到webservice已经调用成功了,我们拿到了返回结果 下面我把测试类贴出来
UseHttpPost use=new UseHttpPost();
try {
InputStream in=this.getClass().getClassLoader().getResourceAsStream("soap.xml");
use.soapService(in, "13764928990"); } catch (Exception e) {
Log.e(TAG, e.getMessage());
}
WebService数据示例的更多相关文章
- php中创建和调用webservice接口示例
php中创建和调用webservice接口示例 这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...
- webservice接口示例(spring+xfire+webservice)
webservice接口示例(spring+xfire+webservice) CreateTime--2018年4月2日17:36:07 Author:Marydon 一.准备工作 1.1 ja ...
- docker-compose 构建mongodb并导入基础数据示例
使用docker-compose构建mongodb服务并导入基础数据示例. 1.文件目录结构 ——mongo/ |——docker-compose.yml |——mongo-Dockerfile |— ...
- 使用poi读取excel数据示例
使用poi读取excel数据示例 分两种情况: 一种读取指定单元格的值 另一种是读取整行的值 依赖包: <dependency> <groupId>org.apache.poi ...
- .NET 5/.NET Core使用EF Core 5连接MySQL数据库写入/读取数据示例教程
本文首发于<.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程> 前言 在.NET Core/. ...
- PHP使用SOAP调用.net的WebService数据
需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路 ...
- qt qml ajax 获取 json 天气数据示例
依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...
- 基于Http替补新闻WebService数据交换
该系统的工作之间的相互作用.随着信息化建设的发展,而业界SOA了解并带来低TOC(总拥有成本)其他优势.越来越多的高层次的信息使用者关注. 这里暂且不提SOA这种架构规划.在系统间集成协议简单的讨论. ...
- 简单的C#TCP协议收发数据示例
参考:http://www.cnblogs.com/jzxx/p/5630516.html 一.原作者的这段话很好,先引用一下: Socket的Send方法,并非大家想象中的从一个端口发送消息到另一个 ...
随机推荐
- Quick-Cocos2dx-Community_3.6.3_Release 编译时libtiff.lib 无法解析
Quick-Cocos2dx-Community_3.6.3_Release 使用VS2012编译,报错: libtiff.lib lnk2001 无法解析的外部符号 ltod3 类似于上面这种,刚才 ...
- quick-cocos2dx 悬浮节点(NotificationNode)
cocos2dx 开发游戏时,有时某些节点不需要随着场景的切换而销毁.但cocos2dx的机制只允许同时只有一个运行的场景,如果你的所有节点都是依附于这个场景的,那场景的切换必然带来节点的销毁. 比如 ...
- 6、MySQL索引种类
1.普通索引 这是最基本的索引,它没有任何限制,比如上文中为title字段创建的索引就是一个普通索引,MyIASM中默认的BTREE类型的索引,也是我们大多数情况下用到的索引. –直接创建索引 CRE ...
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- eclipse执行maven install命令时跳过test
在pom.xml里面配置一下代码,将跳过test. <plugins> <plugin> <groupId>org.apache.maven.plugins< ...
- 一种通过HTTP传文件出网的姿势
在外网机器上运行文件服务接收服务 root@kali:~/pentest-script/FileTransfer/HttpServer# python3 SimpleHttpUpload.py Ser ...
- shell下在while循环中使用ssh命令的问题
1 现象描述 最近使用ssh批量执行命令(已经做了密钥互信了),脚本读取配置文件中的主机列表(内容为每行一台主机IP地址),然后执行,可是每次只是执行第一台,就退出循环了. 2 排查思路 由于脚本比较 ...
- Django rest framework 限制访问频率(源码分析)
基于 http://www.cnblogs.com/ctztake/p/8419059.html 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处 ...
- 封装ajax方法
function ajaxRequest(type, url, data, callback, loading, cache) { var ajaxConfig = { url: '', data: ...
- 安卓屏幕旋转时,禁止Activity重新加载
安卓设备旋转屏幕时,Activity默认会重新加载,如果是要读取大量数据的场景,那等待的时间比较长,这一点不可接受,所以要想办法禁止Activity自动重新加载. 方法如下在AndroidManife ...