要訪問的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);//SoaphttpConn.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等