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;
	}

}