java实现手机号码归属地查询功能

1 package com.rocky.test; java.io.InputStream; 4 import java.io.OutputStream; 5 import java.io.OutputStreamWriter; 6 import java.net.URL; 7 import java.net.URLConnection; javax.xml.parsers.DocumentBuilder; 10 import javax.xml.parsers.DocumentBuilderFactory; org.w3c.dom.NodeList; Moblie {* 16 * 获得soap请求 17 * mobileCode 19 * String getSoapRequest(String mobileCode) { 24 25StringBuilder sb = new StringBuilder(); 26 27sb.append(“<?xml version=\”1.0\” encoding=\”utf-8\”?>” 28+ “\n” 29 30+ “<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”” 31+ ” ” 32 33+ “xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\”” 34+ ” ” 35 36+ “xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”>” 37+ “\n” 38 39+ “<soap:Body>” + “\n” 40 41+ “<getMobileCodeInfo” + ” ” 42+ “xmlns=\”http://WebXml.com.cn/\”>” + “\n” 43 44+ “<mobileCode>” + mobileCode + “</mobileCode>” + “\n” 45 46+ “<userID></userID>” + “\n” 47 48+ “</getMobileCodeInfo>” + “\n” 49 50+ “</soap:Body>” + “\n” 51 52+ “</soap:Envelope>” 53 54 ); sb.toString(); 57 58 }* 62 * 发送soap请求到服务器,服务器空间,香港服务器,并接受返回数据 63 * mobileCode 65 * InputStream getSoapInputStream(String mobileCode) { { 72 73String soap = getSoapRequest(mobileCode);(soap == null); 78 79URL url = new URL( 80″http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx”); 81 82URLConnection conn = url.openConnection(); 83 84conn.setUseCaches(false); 85 86conn.setDoInput(true); 87 88conn.setDoOutput(true); 89 90conn.setRequestProperty(“Content-Type”, “text/xml; charset=utf-8”); 91 92conn.setRequestProperty(“Content-Length”, Integer.toString(soap 93 .length())); 94 95conn.setRequestProperty(“SOAPAction”, 96″http://WebXml.com.cn/getMobileCodeInfo”); 97 98OutputStream os = conn.getOutputStream(); 99 100OutputStreamWriter osw = new OutputStreamWriter(os, “UTF-8”);101 102 osw.write(soap);103 104 osw.flush();105 106 osw.close();107 108InputStream is = conn.getInputStream(); is;111 112} catch (Exception e) {113 114 e.printStackTrace();;117 118 }119 120 } String getMobileNoTrack(String mobileCode) { {125 126org.w3c.dom.Document document = null;127 128DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();129 130dbf.setNamespaceAware(true);131 132InputStream is = getSoapInputStream(mobileCode);133 134DocumentBuilder db = dbf.newDocumentBuilder();135 136document = db.parse(is);137 138NodeList nl = document139.getElementsByTagName(“getMobileCodeInfoResult”);140 141StringBuffer sb = new StringBuffer();(int i = 0; i < nl.getLength(); i++) {144 145org.w3c.dom.Node n = nl.item(i);(n.getFirstChild().getNodeValue().equals(“手机号码错误”)) {148 149sb = new StringBuffer(“#”);150 151System.out.println(“手机号码输入有误”);;154 155 }156 157sb.append(n.getFirstChild().getNodeValue() + “\n”);158 159 }160 161 is.close(); sb.toString();164 165} catch (Exception e) {166 167 e.printStackTrace();;170 171 } } main(String[] args) {176// System.out.println(Moblie.getSoapRequest(“13272303204”));System.out.println(Moblie.getMobileNoTrack(“13226678785”));179 }},香港服务器租用人的价值,在遭受诱-惑的一瞬间被决定

java实现手机号码归属地查询功能

相关文章:

你感兴趣的文章:

标签云: