HttpURLConnection 進行WebService請求

package com.soap.client;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * WebService 發起請求
 * 
 * @author Roger
 */
public class SoapRequestTest {

	public static void main(String[] args) throws Exception {
		
		// Web Service 請求參數
		StringBuffer soapMessage = new StringBuffer();
		soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		soapMessage.append("<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/\">");
		soapMessage.append("  <soap:Body>");
		soapMessage.append("    <GetBasicInfo xmlns=\"http://www.tech-trans.com.cn/\" />");
		soapMessage.append("  </soap:Body>");
		soapMessage.append("</soap:Envelope>");
		
		String soapAction = "http://www.tech-trans.com.cn/GetBasicInfo";

		// 進行soap請求並獲取響應信息
		String response = soapRequest(soapMessage.toString(), soapAction);
		System.out.println(response);
	}

	/**
	 * Web Service 請求
	 * 
	 * @param soapMessage
	 *            請求參數
	 * @param soapAction
	 *            請求soapAction
	 * @return 響應XML
	 */
	public static String soapRequest(String soapMessage, String soapAction) {
		// 請求響應報文
		StringBuilder response = new StringBuilder();

		HttpURLConnection conn = null;
		try {
			conn = getConnection();
			// 設置請求Header
			conn.setRequestProperty("Content-Length",
					String.valueOf(soapMessage.length()));
			conn.setRequestProperty("SOAPAction", soapAction);
			
			// request輸出流
			OutputStream output = conn.getOutputStream();
			if (null != soapMessage) {
				byte[] b = soapMessage.getBytes("utf-8");
				// 發送soap請求報文
				output.write(b, 0, b.length);
			}
			output.flush();
			output.close();

			// 獲取soap響應報文
			InputStream input = conn.getInputStream();
			
			getResponseMessage(input, response);

			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return response.toString();
	}

	/**
	 * 獲取響應數據,進行轉碼,處理亂碼
	 * 
	 * @param input
	 *            InputStream
	 * @param response
	 *            響應數據
	 * @throws Exception
	 */
	public static void getResponseMessage(InputStream input,
			StringBuilder response) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(input,
				"UTF-8"));
		int c = -1;
		// sb爲返回的soap響應報文字符串
		while (-1 != (c = br.read())) {
			response.append((char) c);
		}
	}

	/**
	 * 獲取HttpURLConnection
	 * 
	 * @return HttpURLConnection
	 * @throws Exception
	 */
	public static HttpURLConnection getConnection() throws Exception {
		// 設置soap請求報文的相關屬性
		// 服務端WSDL
		URL url = new URL("http://192.168.1.1:1999/CRM_VIP_Proxy.asmx?WSDL");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 設置請求Header
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setDefaultUseCaches(false);
		conn.setRequestProperty("Host", "58.42.235.140");
		conn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
		conn.setRequestProperty("POST", "/CRM_VIP_Proxy.asmx HTTP/1.1");
		return conn;
	}

}
相關文章
相關標籤/搜索