首先什么是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. JavaScript循环出现的问题——用闭包来解决

    在for循环中,数组长度为3,我本来是想对每个循环的元素绑定一个点击事件的,结果点击后控制台输出全部为1. for (var i = 0; i < data.data.length; i++) ...

  2. Java连载16-++传参&关系运算符

    一.++再举例 int a = 10; System.out.print(a++);//这里会打印出10,因为他们内部这个print函数有参数相当于参数x=a++ System.out.println ...

  3. Rikka with Game[技巧]----2019 杭电多校第九场:1005

      Rikka with Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Othe ...

  4. NVIDIA: Failed to initialize NVML: driver/library version mismatch

    [NVIDIA驱动:Failed to initialize NVML: driver/library version mismatch] 原因:Ubuntu16.04 装新驱动时,会报以上错误,定位 ...

  5. Java中访问修饰符public、private、protecte、default

    Java中访问修饰符public.private.protecte.default的意义讲解:public: Java语言中访问限制最宽的修饰符,一般称之为“公共的”.被其修饰的类.属性以及方法不 仅 ...

  6. vscode 代码补全工具之aiXcoder

    突然发现了一个好用的代码补全工具,与人工智能相关,具有自学习能力,据说用的越久补全效果越好,可以帮助我们节省掉好多敲代码的时间,所以这么好的工具当然要分享给大家了.废话不多说,直接上vscode的安装 ...

  7. [Python] 通过采集23万条数据,对《哪吒》影评分析

    一.说明 数据来源:猫眼: 运行环境:Win10/Python3.7 和 Win7/Python3.5: 分析工具:jieba.WorldCloud.pyecharts和matplotlib: 程序基 ...

  8. 【原创】Linux PSCI框架

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  9. 互联网从此没有 BAT

    根据 Wind 数据截止2019年8月30日,中国十大互联网上市公司排名中,百度排名第 6 位市值 365 亿美元,阿里巴巴排名第一市值高达 4499 亿美元,腾讯排名第二市值 3951 亿美元. 1 ...

  10. Mysql主从同步配置方案(Centos7)

    最近在做项目高可用时,需要使用数据同步.由于只有双节点,且采用主主同步可能存在循环同步的风险,故综合考虑采用Mysql主从同步(Master-Slave同步). 可能没有接触过Mysql数据同步时,可 ...