1 package com.rocky.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 public class Moblie {
14 /**
15 *
16 * 获得soap请求
17 *
18 * @param mobileCode
19 *
20 * @return
21 */
22
23 private static String getSoapRequest(String mobileCode) {
24
25 StringBuilder sb = new StringBuilder();
26
27 sb.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 );
55
56 return sb.toString();
57
58 }
59
60 /**
61 *
62 * 发送soap请求到服务器,并接受返回数据
63 *
64 * @param mobileCode
65 *
66 * @return
67 */
68
69 private static InputStream getSoapInputStream(String mobileCode) {
70
71 try {
72
73 String soap = getSoapRequest(mobileCode);
74
75 if (soap == null)
76
77 return null;
78
79 URL url = new URL(
80 "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx");
81
82 URLConnection conn = url.openConnection();
83
84 conn.setUseCaches(false);
85
86 conn.setDoInput(true);
87
88 conn.setDoOutput(true);
89
90 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
91
92 conn.setRequestProperty("Content-Length", Integer.toString(soap
93 .length()));
94
95 conn.setRequestProperty("SOAPAction",
96 "http://WebXml.com.cn/getMobileCodeInfo");
97
98 OutputStream os = conn.getOutputStream();
99
100 OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
101
102 osw.write(soap);
103
104 osw.flush();
105
106 osw.close();
107
108 InputStream is = conn.getInputStream();
109
110 return is;
111
112 } catch (Exception e) {
113
114 e.printStackTrace();
115
116 return null;
117
118 }
119
120 }
121
122 public static String getMobileNoTrack(String mobileCode) {
123
124 try {
125
126 org.w3c.dom.Document document = null;
127
128 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
129
130 dbf.setNamespaceAware(true);
131
132 InputStream is = getSoapInputStream(mobileCode);
133
134 DocumentBuilder db = dbf.newDocumentBuilder();
135
136 document = db.parse(is);
137
138 NodeList nl = document
139 .getElementsByTagName("getMobileCodeInfoResult");
140
141 StringBuffer sb = new StringBuffer();
142
143 for (int i = 0; i < nl.getLength(); i++) {
144
145 org.w3c.dom.Node n = nl.item(i);
146
147 if (n.getFirstChild().getNodeValue().equals("手机号码错误")) {
148
149 sb = new StringBuffer("#");
150
151 System.out.println("手机号码输入有误");
152
153 break;
154
155 }
156
157 sb.append(n.getFirstChild().getNodeValue() + "\n");
158
159 }
160
161 is.close();
162
163 return sb.toString();
164
165 } catch (Exception e) {
166
167 e.printStackTrace();
168
169 return null;
170
171 }
172
173
174 }
175 public static void main(String[] args) {
176 // System.out.println(Moblie.getSoapRequest("13272303204"));
177 //System.out.println(Moblie.getSoapInputStream("13226678785"));
178 System.out.println(Moblie.getMobileNoTrack("13226678785"));
179 }
180
181
182 }