InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("person.xml");
byte[] data = StreamTool.read(inStream);
String path = "http://192.168.1.100:8080/web/XmlServlet";
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.getOutputStream().write(data);
if(conn.getResponseCode() == 200){
System.out.println("发送成功");
}else{
System.out.println("发送失败");
}
}
person.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>张明</name>
<age>30</age>
</person>
<person id="20">
<name>zhangxiaoxiao</name>
<age>25</age>
</person>
</persons>
调用webservice
/**
* 获取手机号归属地
* @param mobile 手机号
* @return
* @throws Exception
*/
public static String getAddress(String mobile) throws Exception{
String soap = readSoap();
soap = soap.replaceAll("\\$mobile", mobile);
byte[] entity = soap.getBytes();
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if(conn.getResponseCode() == 200){
return parseSOAP(conn.getInputStream());
}
return null;
}
/*
<?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>
<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>string</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap12:Body>
</soap12:Envelope>
*/
private static String parseSOAP(InputStream xml)throws Exception{
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
int event = pullParser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
if("getMobileCodeInfoResult".equals(pullParser.getName())){
return pullParser.nextText();
}
break;
}
event = pullParser.next();
}
return null;
}
private static String readSoap() throws Exception{
InputStream inStream = AddressService.class.getClassLoader().getResourceAsStream("soap12.xml");
byte[] data = StreamTool.read(inStream);
return new String(data);
}
soap12.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>