码迷,mamicode.com
首页 > 移动开发 > 详细

java获取手机号归属地

时间:2017-01-12 03:00:41      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:public   class   org   []   vax   .com   document   turn   ace   

  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 
 57         return sb.toString();
 58 
 59     }
 60 
 61     /**
 62      * 
 63      * 发送soap请求到服务器,并接受返回数据
 64      * 
 65      * @param mobileCode
 66      * 
 67      * @return
 68      */
 69 
 70     private static InputStream getSoapInputStream(String mobileCode) {
 71 
 72         try {
 73 
 74             String soap = getSoapRequest(mobileCode);
 75 
 76             if (soap == null)
 77 
 78                 return null;
 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();
100 
101             OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
102 
103             osw.write(soap);
104 
105             osw.flush();
106 
107             osw.close();
108 
109             InputStream is = conn.getInputStream();
110 
111             return is;
112 
113         } catch (Exception e) {
114 
115             e.printStackTrace();
116 
117             return null;
118 
119         }
120 
121     }
122 
123     public static String getMobileNoTrack(String mobileCode) {
124 
125         try {
126 
127             org.w3c.dom.Document document = null;
128 
129             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
130 
131             dbf.setNamespaceAware(true);
132 
133             InputStream is = getSoapInputStream(mobileCode);
134 
135             DocumentBuilder db = dbf.newDocumentBuilder();
136 
137             document = db.parse(is);
138 
139             NodeList nl = document
140                     .getElementsByTagName("getMobileCodeInfoResult");
141 
142             StringBuffer sb = new StringBuffer();
143 
144             for (int i = 0; i < nl.getLength(); i++) {
145 
146                 org.w3c.dom.Node n = nl.item(i);
147 
148                 if (n.getFirstChild().getNodeValue().equals("手机号码错误")) {
149 
150                     sb = new StringBuffer("#");
151 
152                     System.out.println("手机号码输入有误");
153 
154                     break;
155 
156                 }
157 
158                 sb.append(n.getFirstChild().getNodeValue() + "\n");
159 
160             }
161 
162             is.close();
163 
164             return sb.toString();
165 
166         } catch (Exception e) {
167 
168             e.printStackTrace();
169 
170             return null;
171 
172         }
173         
174 
175     }
176     public static void 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     }
181     
182 
183 }

代码直接复制就OK

java获取手机号归属地

标签:public   class   org   []   vax   .com   document   turn   ace   

原文地址:http://www.cnblogs.com/fengzikuange/p/6274231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!