webservice客户端

package demo.order.client;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.spi.Provider;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


public class ProxyClient {

	public static void main(String args[]) throws Exception {
		// excuteWebService1();
		 excuteWebService2();
		 //excuteWebService3() ;
		// excuteWebService4();
	}

	public static void excuteWebService4() throws MalformedURLException  {
		QName SERVICE = new QName("http://market.integration.service.sggis.com", "MarketService");
		QName UserServiceImplPort =new QName("http://market.integration.service.sggis.com", "marketServiceWSImplPortType");  
		URL url = new URL("http://127.0.0.1:8001/EpgisServer/services/MarketService?wsdl");
		javax.xml.ws.spi.ServiceDelegate dele = Provider.provider().createServiceDelegate(url, SERVICE, Service.class);
		IMarketServiceWS us = (IMarketServiceWS) dele.getPort(UserServiceImplPort, IMarketServiceWS.class);
		String content = "<gt:GetLayersInfoExInput xmlns:gt=\"http://www.sgcc.com.cn/sggis/service/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd\">" + "<gt:Token>3B332567-6D9E-41EF-9B99-9FFA30D88962</gt:Token></gt:GetLayersInfoExInput>";
		String res = us.getLayersInfoEx(content);
		System.out.println(res);
	}

	public static void excuteWebService3() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://127.0.0.1:8001/EpgisServer/services/MarketService");
		QName SERVICE = new QName("http://market.integration.service.sggis.com", "IMarketServiceWS");
		factory.setServiceName(SERVICE);
		factory.setServiceClass(IMarketServiceWS.class);
		IMarketServiceWS us = (IMarketServiceWS) factory.create();
		String content = "<gt:GetLayersInfoExInput xmlns:gt=\"http://www.sgcc.com.cn/sggis/service/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd\">" + "<gt:Token>3B332567-6D9E-41EF-9B99-9FFA30D88962</gt:Token></gt:GetLayersInfoExInput>";
		String res = us.getLayersInfoEx(content);
		System.out.println(res);

	}

	private static void excuteWebService2() throws Exception {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient("http://127.0.0.1:8001/EpgisServer/services/MarketService?wsdl");
		String content = "<gt:GetLayersInfoExInput xmlns:gt=\"http://www.sgcc.com.cn/sggis/service/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd\">" + "<gt:Token>3B332567-6D9E-41EF-9B99-9FFA30D88962</gt:Token></gt:GetLayersInfoExInput>";
		Object[] objects = client.invoke("getLayersInfoEx", content);
		if (objects != null && objects.length > 0) {
			for (Object province : objects) {
				System.out.println(province.toString());
			}
		}
	}

	/**
	 * 调用webservice
	 */
	public static void excuteWebService1() {
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod httpMethod = new PostMethod("http://127.0.0.1:8001/EpgisServer/services/MarketService");
			httpMethod.addRequestHeader("content-type", "text/xml;charset=UTF-8");
			httpMethod.addRequestHeader("soapaction", "");
			httpMethod.setContentChunked(true);
			String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:mar=\"http://market.integration.service.sggis.com\">" + "<soapenv:Header/>   <soapenv:Body>      <mar:getLayersInfoEx><mar:inputXML> <![CDATA[" + " <gt:GetLayersInfoExInput xmlns:gt=\"http://www.sgcc.com.cn/sggis/service/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd\"> " + " <gt:Token>3B332567-6D9E-41EF-9B99-9FFA30D88962</gt:Token> </gt:GetLayersInfoExInput>         ]]>" + " </mar:inputXML>      </mar:getLayersInfoEx>   </soapenv:Body></soapenv:Envelope>";
			InputStream in = new ByteArrayInputStream(content.getBytes("utf-8"));
			RequestEntity entity = new InputStreamRequestEntity(in);
			httpMethod.setRequestEntity(entity);
			int statusCode = httpClient.executeMethod(httpMethod);
			if (statusCode != 200) {
				System.out.println("状态码:" + statusCode);
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(httpMethod.getResponseBodyAsStream(), "ISO-8859-1"));
			String tmp = null;
			StringBuilder htmlRet = new StringBuilder();
			while ((tmp = reader.readLine()) != null) {
				htmlRet.append(tmp);
			}
			reader.close();
			System.out.println(new String(htmlRet.toString().getBytes("ISO-8859-1"), "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

猜你喜欢

转载自action-java.iteye.com/blog/2198384