package com.zf.test;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class QueryMobilPhonAddress {
/**
* 根据URL 和 手机号 得到归属地信息的InputStream对象
* @param number 手机号
* @return
* @throws Exception
*/
public static InputStream getInputStream(long number) throws Exception{
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=" + number + "&userID=");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
return conn.getInputStream();
}
/**
* 根据输入流生成XMl文档 ,然后通过DOM解析XML文档 得到 手机号 即 归属地 字符串类型详细信息
* @param is 输入流对象
* @return
* @throws Exception
*/
public static NumberInfo parseIputStreamToXML(InputStream is) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document = factory.newDocumentBuilder().parse(is);
Node node = document.getFirstChild();
String content = node.getFirstChild().getNodeValue();
String[] contentInfo = content.split(":");
NumberInfo numberInfo = new NumberInfo(Long.parseLong(contentInfo[0]) ,contentInfo[1] );
return numberInfo;
}
public static void main(String[] args) throws Exception {
NumberInfo numberInfo = parseIputStreamToXML(getInputStream(13145955568L));
System.out.println(numberInfo);
}
}
class NumberInfo{
private long number ;
private String numberAddress;
public NumberInfo() {}
public NumberInfo(long number, String numberAddress) {
this.number = number;
this.numberAddress = numberAddress;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public String getNumberAddress() {
return numberAddress;
}
public void setNumberAddress(String numberAddress) {
this.numberAddress = numberAddress;
}
@Override
public String toString() {
return "手机号:" + number + "\n归属地:"+ numberAddress ;
}
}
根据手机号 查询手机归属地信息
最新推荐文章于 2023-02-15 18:20:16 发布