E:/workplace/java/website/src/main/resources/weather.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>{mobileCode}</mobileCode> <userID>{userID}</userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>
读取实例:
public static void main(String[] args) throws IOException {
File file = new File("E:/workplace/java/website/src/main/resources/weather.xml");
String xml = FileUtil.readAsString(file);
xml= xml.replace("{mobileCode}", "15001279241");
xml = xml.replace("{userID}", "");
byte[] bytes = xml.getBytes();
String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
connection.setRequestProperty("Content-Length",String.valueOf( bytes.length));
connection.setRequestMethod("POST");
connection.getOutputStream().write(bytes);
if(connection.getResponseCode() == 200){
InputStream ins = connection.getInputStream();
byte[] bts = new byte[ins.available()];
ins.read(bts);
String tHtmlBody = new String(bts, "utf-8");
//解析xml
System.out.println(tHtmlBody);
}
}