调用webservice查询手机号归属地

public static String getAddress(String mobildeCode) {
        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        String soap = readSoap();
        soap = soap.replaceAll("\\$mobile", mobildeCode);
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(path)
                    .openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type",
                    "application/soap+xml; charset=utf-8");
            conn.setRequestProperty("Content-Length",
                    String.valueOf(soap.length()));
            conn.getOutputStream().write(soap.getBytes());
            if (conn.getResponseCode() == 200) {
                String result = parseSOAP(conn.getInputStream());
                return result;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
private static String parseSOAP(InputStream xml) throws Exception {
        // TODO Auto-generated method stub
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(xml, "UTF-8");
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_TAG:
                if("getMobileCodeInfoResult".equals(parser.getName())){
                    return parser.nextText();
                }
                break;
            }
            event = parser.next();
        }
        return null;
    }

    private static String readSoap() {
        InputStream stream = LoginService.class.getClassLoader()
                .getResourceAsStream("soap.xml");
        String str = readStreamToStr(stream);
        return str;
    }

    private static String readStreamToStr(InputStream stream) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        try {
            while ((len = stream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            String result = new String(baos.toByteArray(), "UTF-8");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//soap.xml
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值