如题,实现手机号码归属地查询!- 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;
- }
- }
-
复制代码
|
|