要访问的webservice服务说明文档:
根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName
1.请求soap1.1
1.1.java文件
package soap; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class TestSoap1_1 { public static void main(String[] args) throws Exception { String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; String xmlFile = "soap1.1.xml";// 要发送的soap格式文件 String soapActionString = "http://WebXml.com.cn/getWeatherbyCityName";//Soap 1.1中使用 URL url = new URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); File fileToSend = new File(xmlFile); byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组 new FileInputStream(xmlFile).read(buf); // httpConn.setRequestProperty("Content-Length", // String.valueOf(buf.length));//这句话可以不用写,即使是随便写 //根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算 httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestProperty("soapActionString",soapActionString);//Soap httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); OutputStream out = httpConn.getOutputStream(); out.write(buf); out.close(); InputStreamReader is = new InputStreamReader(httpConn.getInputStream(), "utf-8"); BufferedReader in = new BufferedReader(is); String inputLine; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("result.xml")));// 将结果存放的位置 while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); bw.write(inputLine); bw.newLine(); } bw.close(); in.close(); httpConn.disconnect(); } }
1.2.soap1.1.xml
<?xml version="1.0" encoding="utf-8"?> <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/"> <soap:Body> <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> <theCityName>广州</theCityName> </getWeatherbyCityName> </soap:Body> </soap:Envelope>
2.请求soap1.2
在这里说明一下,访问soap1.2似乎很宽松,即使头部的一些信息不符合文档要求也能正常请求到数据
2.1.java文件
package soap; import java.io.*; import java.net.*; import javax.xml.soap.*; public class TestSopa1_2 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; String xmlFile = "soap1.2.xml";// 要发送的soap格式文件 URL url = new URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); File fileToSend = new File(xmlFile); byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组 new FileInputStream(xmlFile).read(buf); // httpConn.setRequestProperty("Content-Length", // String.valueOf(buf.length));//这句话可以不用写,即使是随便写 //根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算 httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); OutputStream out = httpConn.getOutputStream(); out.write(buf); out.close(); InputStreamReader is = new InputStreamReader(httpConn.getInputStream(), "utf-8"); BufferedReader in = new BufferedReader(is); String inputLine; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("result.xml")));// 将结果存放的位置 while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); bw.write(inputLine); bw.newLine(); } bw.close(); in.close(); httpConn.disconnect(); } }
2.2.soap1.2.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> <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> <theCityName>广州</theCityName> </getWeatherbyCityName> </soap12:Body> </soap12:Envelope>
3.建议
最好是下载一个wireshark,因为这样你就能够查看你到底发送什么东西过去了
以下是我发送的信息:
4.设置头信息
如果运行不了,请查看头信息,并且可以使用httpConn.setRequestProperty设置Host,Accept等