首先什么是webservice ?

从广义上面讲,任何一个服务器所提供的"数据","内容","方法"等等都可以理解为webservice. 本例是一个狭义的webservice
本例所使用的webservice地址:http://www.webxml.com.cn/zh_cn/index.aspx
 
1.打开这个网站,然后找到号码归属地
2.进入后会看到getMobileCodeInfogetDatabaseInfo两种获取归属地的方式,前面的是基于SOAP(肥皂协议)协议的;后面的是基于数据库的
本例采用SOAP协议编写,点击getMobileCodeInfo进入相应页面,会看到SOAP1.1/1.2, 这里使用1.2的
SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。 POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-
Content-Length: length <?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>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 OK
Content-Type: application/soap+xml; charset=utf-
Content-Length: length <?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>
<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>string</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap12:Body>
</soap12:Envelope>

根据上面给出的xml得知,第一个xml文件是post要提交的数据,根据提示请求的数据需要用占位符
<mobileCode>$mobile</mobileCode>  这里给号码加入占位符
<userID></userID>          这个号码所对应的唯一编号,可以留空,因为不处理它

3.把post请求的xml文件拷贝到项目中,这里是拷贝到了src下面--post.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>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

具体代码如下NumberService类--处理post请求以及获取服务器返回的归属地信息

public class NumberService {
/**
* 查询号码归属地
*
* @param number
* @return
* @throws Exception
*/
public static String getAddress(String number) throws Exception {
// 把soap肥皂协议-以post方式提交的xml数据获取出来
InputStream is = NumberService.class.getClassLoader()
.getResourceAsStream("data.xml");
byte[] xml = StreamTool.getBytes(is);
String postxml = new String(xml);
//获取将要提交的xml中的 手机号,$mobile为号码占位符 --- 这个xml就相当于 一个jsp表单
String phoneNumber = postxml.replace("$mobile", number);
//以post方式发送数据给服务器
String numberAddress = sendDataByPost(phoneNumber);
return numberAddress;
} /**
* 以post方式请求服务器
*
* @param result
* 参数就是要查询的手机号码
* @return
* @throws Exception
*/
public static String sendDataByPost(String phoneNumber) throws Exception {
URL url = new URL(
"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
String data = phoneNumber; // 要传递的数据
//byte[] bytes = data.getBytes(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时连接
conn.setReadTimeout();
//设置请求方式
conn.setRequestMethod("POST");
//设置post请求的参数
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", data.length()+""); //允许http协议对外输出信息
conn.setDoOutput(true);
//把数据写给服务器
conn.getOutputStream().write(data.getBytes()); int code = conn.getResponseCode();
if(code==){
//获取从服务器中返回过来的流(由于返回的数据是xml格式,所以需要解析xml)
InputStream is = conn.getInputStream();
String address = ResultXml.getAddress(is);
return address;
}else{
return "访问网络出错";
}
}
}

根据上面服务器给出的xml相应文件--需要解析getMobileCodeInfoResult就可以获取手机对属地信息
<?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>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

解析类如下

/**
* 解析服务器返回的xml数据,获取归属地
* @author Administrator
*
*/
public class ResultXml {
public static String getAddress(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int type = parser.getEventType(); while(type!=XmlPullParser.END_DOCUMENT){
if(type==XmlPullParser.START_TAG){
if("getMobileCodeInfoResult".equals(parser.getName())){
return parser.nextText();
}
}
type = parser.next();
}
return "没有该号码的信息";
}
}

上面代码中使用了一个工具类StreamTool

public class StreamTool {
/**
*
* @param is 参数InputStream代表从服务器返回过来的流(流中存储了我们所需的数据)
* @return
* @throws Exception
* 这里把服务器流中的数据循环的写入到了内存数组流ByteArrayOutputStream里面
* 作用是内存操作数据快.最后以字节数组返回
*/
public static byte[] getBytes(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[];
int len = ;
while((len = is.read(buffer))!=-){
bos.write(buffer, , len);
}
is.close();
bos.flush();
byte[] result = bos.toByteArray();
System.out.println(new String(result));
return result;
}
}

测试截图

 

调用webservice获取电话号码归属地信息的更多相关文章

  1. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...

  2. 调用手机在线API获取手机号码归属地信息

    手机在线(www.showji.com)始创于2001年,发展至今已拥有国内最准确.号段容量最大的手机号码归属地数据库系统, 目前号段容量将近33万条,每月保持两次以上规模数据更新,合作伙伴包括:百度 ...

  3. webService获取电话号归属地和获取天气情况步骤,及创建属于自己的webservice

    一.什么是Web服务 Web服务是一种可以用来解决跨网络应用集成问题的开发模式,目的是保证不同平台的应用服务可以互操作 二.Web服务的三个核心 Soap: SOAP(Simple Object Ac ...

  4. 01.阿里云SDK调用,获取ESC主机详细信息

    一:通过python SDK获取云主机的详细信息 1.创建Accessky码(不做展示) 2.通过pip安装SDK模块,这个阿里云帮助里面有,也不做详细展示. 3.详细使用方法看代码 我下面展示的返回 ...

  5. C#调用WebService获取天气信息

    概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...

  6. .NET(C#)调用webService获取客户端IP地址所属区域(非异步)

    功能描述: 此接口用于获取客户端访问的IP的地址所属的区域(国家,城市等).通过输入IP地址查询国家.城市.所有者等信息.没有注明国家的为中国输入参数:IP地址(自动替换 " ." ...

  7. 调用WebService获取数据

    以下调用方法,以调用苏州天气接口为例. 一.后台请求服务 方法一.C#后台,通过构建Soap请求接口数据 //获取天气详细信息 public JsonResult GetWeatherDetails( ...

  8. 第十五章:Android 调用WebService(.net平台)

    什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...

  9. Android 调用webService(.net平台)

    什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...

随机推荐

  1. 非常实用的select下拉框-Select2.js

    在Web开发中,Select下拉框是常用的输入组件.由于原生的Select几乎很难通过CSS样式控制.一些好看的Select下拉框就只能通过模拟来实现.PHP程序员雷雪松给大家推荐一筐款不错的Sele ...

  2. Android8.1 MTK平台 截屏功能分析

    前言 涉及到的源码有 frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java vend ...

  3. Sqlserver 锁表查询代码记录

    --方法1WITH CTE_SID ( BSID, SID, sql_handle ) AS ( SELECT blocking_session_id , session_id , sql_handl ...

  4. jmeter之beanshell使用

    beanshell官网:http://www.BeanShell.org/ 一.beanshell介绍 是一种完全符合Java语法规范的轻量级的脚本语言: 相当于一个小巧免费嵌入式的Java源代码解释 ...

  5. 三维动画形变算法(Laplacian-Based Deformation)

    网格上顶点的Laplace坐标(均匀权重)定义为:,其中di为顶点vi的1环邻域顶点数. 网格Laplace坐标可以用矩阵形式表示:△=LV,其中,那么根据网格的Laplace坐标通过求解稀疏线性方程 ...

  6. Maven项目添加ojdbc8

    1.找到Oracle中的ojdbc8,它的位置在Oracle客户端 2.找到它的位置后,把你放ojdbc8的位置复制,改如下代码"D:\ojdbc8.jar"为你的ojdbc8位置 ...

  7. python 35 多线程

    目录 多线程 1. 线程 2. 线程vs进程 3. 开启线程的两种方法. 4. 线程的特性 5. 线程的相关方法 6. join 阻塞 7. 守护线程 daemon 8. 互斥锁 多线程 1. 线程 ...

  8. Android自动化测试探索(五)代码覆盖率统计

    Android 代码覆盖率统计 本周开始准备统计Android自动化用例的代码覆盖率,将最终使用的方法记录下来. 覆盖率监测的原理 覆盖率监测的原理跟iOS上的原理差不多,大致的思路参考下吧, iOS ...

  9. HDU-6356 Glad You Came 线段树 ST表

    HDU-6356 题意:有m次操作,每次操作通过给定的随机函数生成 l , r , v,使得在 l 到 r 区间内,所有的a[i]变为max(a[i] , v). 最后输出n个a[i]* i的异或和. ...

  10. 区间dp专题

    HDU4283You Are the One区间dp, 记忆话搜索运行时间:   #include <iostream> #include <cstdio> #include ...