package test.xt.hb.map;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.junit.Test;

public class GpsTest {
	public void testUrl() {
	}

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws HttpException
	 */
	@Test
	public void testHttpClientWsdl() throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(
				"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "
						+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
		sb.append("<SOAP-ENV:Body>");
		sb.append("<m:GetGPSRealTime xmlns:m=\"http://pdiwt.com.cn/webservices/Oracle\"/>");
		sb.append("</SOAP-ENV:Body>");
		sb.append("</SOAP-ENV:Envelope>");
		String soapRequestData =sb.toString();
		System.out.println(soapRequestData);
		PostMethod postMethod = new PostMethod("http://192.168.41.14:8000/Service.asmx?wsdl");
		// 然后把Soap请求数据添加到PostMethod中
		byte[] b = soapRequestData.getBytes();
		InputStream is = new ByteArrayInputStream(b, 0, b.length);
		RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/html; charset=utf-8");
		postMethod.setRequestEntity(re);

		// 最后生成一个HttpClient对象，并发出postMethod请求
		HttpClient httpClient = new HttpClient();
		int statusCode = httpClient.executeMethod(postMethod);
		if (statusCode == 200) {
			System.out.println("调用成功！");
			String soapResponseData = postMethod.getResponseBodyAsString();
			System.out.println(soapResponseData);
		} else {
			System.out.println("调用失败！错误码：" + statusCode);
		}

	}

}