您的位置:首页 > 其它

调用webservice查询手机号归属地

2015-04-02 15:08 183 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息