java 读取webservice,java读取手机号码归属地信息

本文介绍了一个使用Java调用WebService服务的具体实例。通过构造SOAP请求并发送到指定URL,展示了如何实现移动号码信息的查询功能。文中包含了从XML文件读取请求模板、替换变量、设置HTTP请求头到解析返回结果的全过程。

 

 E:/workplace/java/website/src/main/resources/weather.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>{mobileCode}</mobileCode>
      <userID>{userID}</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

 读取实例:

public static void main(String[] args) throws IOException {
		 File file = new File("E:/workplace/java/website/src/main/resources/weather.xml");
		 String xml = FileUtil.readAsString(file);
		 xml= xml.replace("{mobileCode}", "15001279241");
		 xml = xml.replace("{userID}", "");
		 
		 byte[] bytes = xml.getBytes();
		
		 
		 
		String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		
		 URL url = new URL(path);
		 HttpURLConnection connection =  (HttpURLConnection) url.openConnection();
		 connection.setConnectTimeout(5000);
		 connection.setDoOutput(true);
		 connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		 connection.setRequestProperty("Content-Length",String.valueOf( bytes.length));
		 connection.setRequestMethod("POST");
		 connection.getOutputStream().write(bytes);
		 
		 if(connection.getResponseCode() == 200){
			 InputStream ins = connection.getInputStream();
			 byte[] bts = new byte[ins.available()];
			 ins.read(bts);
			 String	 tHtmlBody = new String(bts, "utf-8");
			 //解析xml
			 System.out.println(tHtmlBody);
			 
		 }
		 
		
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值