首页 / JAVA / java获取手机号归属地
java获取手机号归属地
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java获取手机号归属地,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3616字,纯文字阅读大概需要6分钟。
内容图文
![java获取手机号归属地](/upload/InfoBanner/zyjiaocheng/1158/f36b1c23df4142aebe11c53df0b32d7c.jpg)
1 package org.lebing.test; 2 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.io.OutputStreamWriter; 6 import java.net.URL; 7 import java.net.URLConnection; 8 9 import javax.xml.parsers.DocumentBuilder; 10 import javax.xml.parsers.DocumentBuilderFactory; 11 12 import org.w3c.dom.NodeList; 13 14 public class Test { 15 /** 16 * 17 * 获得soap请求 18 * 19 * @param mobileCode 20 * 21 * @return 22 */ 23 24 private static String getSoapRequest(String mobileCode) { 25 26 StringBuilder sb = new StringBuilder(); 27 28 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" 29 + "\n" 30 31 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 32 + " " 33 34 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" 35 + " " 36 37 + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 38 + "\n" 39 40 + "<soap:Body>" + "\n" 41 42 + "<getMobileCodeInfo" + " " 43 + "xmlns=\"http://WebXml.com.cn/\">" + "\n" 44 45 + "<mobileCode>" + mobileCode + "</mobileCode>" + "\n" 46 47 + "<userID></userID>" + "\n" 48 49 + "</getMobileCodeInfo>" + "\n" 50 51 + "</soap:Body>" + "\n" 52 53 + "</soap:Envelope>" 54 55 ); 56 57return sb.toString(); 58 59 } 60 61/** 62 * 63 * 发送soap请求到服务器,并接受返回数据 64 * 65 * @param mobileCode 66 * 67 * @return 68*/ 69 70privatestatic InputStream getSoapInputStream(String mobileCode) { 71 72try { 73 74 String soap = getSoapRequest(mobileCode); 75 76if (soap == null) 77 78returnnull; 79 80 URL url = new URL( 81 "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx"); 82 83 URLConnection conn = url.openConnection(); 84 85 conn.setUseCaches(false); 86 87 conn.setDoInput(true); 88 89 conn.setDoOutput(true); 90 91 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 92 93 conn.setRequestProperty("Content-Length", Integer.toString(soap 94 .length())); 95 96 conn.setRequestProperty("SOAPAction", 97 "http://WebXml.com.cn/getMobileCodeInfo"); 98 99 OutputStream os = conn.getOutputStream(); 100101 OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); 102103 osw.write(soap); 104105 osw.flush(); 106107 osw.close(); 108109 InputStream is = conn.getInputStream(); 110111return is; 112113 } catch (Exception e) { 114115 e.printStackTrace(); 116117returnnull; 118119 } 120121 } 122123publicstatic String getMobileNoTrack(String mobileCode) { 124125try { 126127 org.w3c.dom.Document document = null; 128129 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 130131 dbf.setNamespaceAware(true); 132133 InputStream is = getSoapInputStream(mobileCode); 134135 DocumentBuilder db = dbf.newDocumentBuilder(); 136137 document = db.parse(is); 138139 NodeList nl = document 140 .getElementsByTagName("getMobileCodeInfoResult"); 141142 StringBuffer sb = new StringBuffer(); 143144for (int i = 0; i < nl.getLength(); i++) { 145146 org.w3c.dom.Node n = nl.item(i); 147148if (n.getFirstChild().getNodeValue().equals("手机号码错误")) { 149150 sb = new StringBuffer("#"); 151152 System.out.println("手机号码输入有误"); 153154break; 155156 } 157158 sb.append(n.getFirstChild().getNodeValue() + "\n"); 159160 } 161162 is.close(); 163164return sb.toString(); 165166 } catch (Exception e) { 167168 e.printStackTrace(); 169170returnnull; 171172 } 173174175 } 176publicstaticvoid main(String[] args) { 177// System.out.println(Test.getSoapRequest("13272303204")); 178// System.out.println(Test.getSoapInputStream("13226678785"));179 System.out.println(Test.getMobileNoTrack("13111111111")); 180 } 181182183 }
代码直接复制就OK
原文:http://www.cnblogs.com/fengzikuange/p/6274231.html
内容总结
以上是互联网集市为您收集整理的java获取手机号归属地全部内容,希望文章能够帮你解决java获取手机号归属地所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。