java调用WebService(客户端)

<p>java调用WebService(客户端)
看下了网上大部分都是写java来编写WS服务端,写了下JAVA的调用端。</p><p>WebService可以有Get、 Post、Soap、Document四种方式调用,以下是四种方式的参照说明。
name 属性 说明
HttpGet 添加HTTP GET协议 在追加到HTTP请求URL的查询字符串中传递的方法参数,格式为:?name1=value1&name2=value2...。返回值被当做简单的XML文档放入HTTP响应的正文中(没有<soap:Envelope>)。
HTTPPost 添加HTTP POST协议 在HTTP请求的正文中传递的方法参数,格式为:name1=value1&name2=value2...。返回值被当做简单的XML文档放入HTTP响应的正文中(没有< soap:Envelope>)。
HTTPSoap 添加Http Soap协议 Soap消息在HTTP请求的正文中发送;Soap响应在HTTP响应的正文中发送。
Documentation 添加特殊的Documentation协议 当在启用了此协议的情况下直接请求.asmx页时,Asp.Net运行Helper页创建HTML文档页,该文档页被传递到提出请求的客户端</p><p>对于SOAP协议多写了个CXF的调用(对于CXF必须有以下包:)</p><p>
以下是调用代码
</p>
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * 功能描述:WebService调用
 * 
 */
public class ClientTest {

	/**
	 * 功能描述:HTTP-GET
	 * 
	 */
	public String get() {
		URL url;
		BufferedReader bin = null;
		StringBuilder result = new StringBuilder();
		try {
			String urlTemp = "http://www.webxml.com.cn//WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName="
					+ URLEncoder.encode("福建", "UTF-8");// URLEncoder用来参数编码
			url = new URL(urlTemp);
			InputStream in = url.openStream(); // 请求
			bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String s = null;
			while ((s = bin.readLine()) != null) {
				result.append(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != bin) {
				try {
					bin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result.toString();
	}

	/**
	 * 功能描述:HTTP-POST
	 * 
	 */
	public String post() {
		OutputStreamWriter out = null;
		StringBuilder sTotalString = new StringBuilder();
		try {
			URL urlTemp = new URL(
					"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity");
			URLConnection connection = urlTemp.openConnection();
			connection.setDoOutput(true);
			out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
			StringBuffer sb = new StringBuffer();
			sb.append("byProvinceName=福建");
			out.write(sb.toString());
			out.flush();
			String sCurrentLine;
			sCurrentLine = "";
			InputStream l_urlStream;
			l_urlStream = connection.getInputStream();// 请求
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(
					l_urlStream));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString.append(sCurrentLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sTotalString.toString();
	}

	/**
	 * 功能描述: 请求 HTTP-SOAP
	 * 
	 */
	public String getSoapInputStream() {
		try {
			String soap = "<?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><getSupportCity xmlns=\"http://WebXml.com.cn/\"><byProvinceName></byProvinceName></getSupportCity></soap:Body></soap:Envelope>";
			URL url = new URL(
					"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
			URLConnection conn = url.openConnection();
			conn.setUseCaches(false);
			conn.setDoInput(true);
			conn.setDoOutput(true);

			conn.setRequestProperty("Content-Length", Integer.toString(soap
					.length()));
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("SOAPAction",
					"http://WebXml.com.cn/getSupportCity");

			OutputStream os = conn.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
			osw.write(soap);
			osw.flush();
			osw.close();
			StringBuilder sTotalString = new StringBuilder();
			String sCurrentLine = "";
			InputStream is = conn.getInputStream();
			BufferedReader l_reader = new BufferedReader(new InputStreamReader(
					is));
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString.append(sCurrentLine);
			}
			return sTotalString.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 功能描述:使用CXF 请求 HTTP-SOAP
	 * 
	 */
	public String soap() {
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
				.newInstance();
		String url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";// http://www.fjyxd.com:17001/DefDispatcher/dispatche?wsdl
		Client clientTemp = clientFactory.createClient(url);
		Object[] arg;
		String result = "";
		try {
			arg = clientTemp.invoke("qqCheckOnline", "8698053");// 查询8698053在线状态,QQ号码
			// String,默认QQ号码:8698053。返回数据:String,Y
			// = 在线;N = 离线;E
			// = QQ号码错误;A =
			// 商业用户验证失败;V =
			// 免费用户超过数量
			result = (String) arg[0];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 功能描述:调用
	 * 
	 */
	public static void main(String[] args) {
		ClientTest ct = new ClientTest();
		System.out.println("HTTP-GET结果:" + ct.get());
		System.out.println("HTTP-POST结果:" + ct.post());
		System.out.println("HTTP-SOAP结果:" + ct.getSoapInputStream());
		System.out.println("CXF HTTP-SOAP结果:" + ct.soap());
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值