天天看點

發送http post請求soap服務

     要通路的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,因為這樣你就能夠檢視你到底發送什麼東西過去了 以下是我發送的資訊:

發送http post請求soap服務
發送http post請求soap服務

4.設定頭資訊

如果運作不了,請檢視頭資訊,并且可以使用httpConn.setRequestProperty設定Host,Accept等

繼續閱讀