發送http post請求soap服務

來源:互聯網
上載者:User
     要訪問的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等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.