加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

调用WebService获取手机号的归属地

发布时间:2020-12-17 00:34:59 所属栏目:安全 来源:网络整理
导读:import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; impo
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Mobile {
?/**
? * 获得soap请求
? *
? * @param mobileCode
? * @return
? */
?private static String getSoapRequest(String mobileCode) {
??StringBuilder sb = new StringBuilder();
??sb
????.append("<?xml version="1.0" encoding="utf-8"?>"
??????+ "n"
??????+ "<soap:Envelope xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance""
??????+ " "
??????+ "xmlns:xsd=" http://www.w3.org/2001/XMLSchema""
??????+ " "
??????+ "xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/">"
??????+ "n"
??????+ "<soap:Body>" + "n"
??????+ "<getMobileCodeInfo" + " "
??????+ "xmlns=" http://WebXml.com.cn/">" + "n"
??????+ "<mobileCode>" + mobileCode + "</mobileCode>" + "n"
??????+ "<userID></userID>" + "n"
??????+ "</getMobileCodeInfo>" + "n"
??????+ "</soap:Body>" + "n"
??????+ "</soap:Envelope>"
????);
??return sb.toString();
?}
?/**
? * 发送soap请求到服务器,并接受返回数据
? *
? * @param mobileCode
? * @return
? */
?private static InputStream getSoapInputStream(String mobileCode) {
??try {
???String soap = getSoapRequest(mobileCode);
???if (soap == null)
????return null;
???URL url = new URL(
?????" http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx");
???URLConnection conn = url.openConnection();
???conn.setUseCaches(false);
???conn.setDoInput(true);
???conn.setDoOutput(true);
???conn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
???conn.setRequestProperty("Content-Length",Integer.toString(soap
?????.length()));
???conn.setRequestProperty("SOAPAction",
?????" http://WebXml.com.cn/getMobileCodeInfo");
???OutputStream os = conn.getOutputStream();
???OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
???osw.write(soap);
???osw.flush();
???osw.close();
???InputStream is = conn.getInputStream();
???return is;
??} catch (Exception e) {
???e.printStackTrace();
???return null;
??}
?}
?public static String getMobileNoTrack(String mobileCode) { ??try { ???Document document = null; ???DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); ???dbf.setNamespaceAware(true); ???InputStream is = getSoapInputStream(mobileCode); ???DocumentBuilder db = dbf.newDocumentBuilder(); ???document = db.parse(is); ???NodeList nl = document ?????.getElementsByTagName("getMobileCodeInfoResult"); ???StringBuffer sb = new StringBuffer(); ???for (int i = 0; i < nl.getLength(); i++) { ????Node n =? nl.item(i); ????if (n.getFirstChild().getNodeValue().equals("手机号码错误")) { ?????sb = new StringBuffer("#"); ?????System.out.println("手机号码输入有误"); ?????break; ????} ????sb.append(n.getFirstChild().getNodeValue() + "n"); ???} ???is.close(); ???return sb.toString(); ??} catch (Exception e) { ???e.printStackTrace(); ???return null; ??} ?} ?public static void main(String[] args) { ??System.out.println(getMobileNoTrack("18801001189")); ??System.out.println(getMobileNoTrack("15173720808")); ?} }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读