当前位置:文档之家› java调用WebService(客户端)

java调用WebService(客户端)

java调用WebService(客户端)看下了网上大部分都是写java来编写WS服务端,小编这边就小写了下JAVA的调用端。

WebService可以有Get、Post、Soap、Document四种方式调用,以下是四种方式的参照说明。

对于SOAP协议多写了个CXF的调用(对于CXF必须有以下包:)以下是调用代码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 .URL;import .URLConnection;import .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 = "//WebServices/WeatherWebService.asmx/getSupportCity?byProvin ceName="+ 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("/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=\"/2001/XMLSchema-instance\"xmlns:xsd=\"/2001/XMLSchema\"xmlns:soap=\"/soap/envelope/\"><soap:Body><getSupportCity xmlns=\"/\"><byProvinceName></byProvinceName></getSupportCity></ soap:Body></soap:Envelope>";URL url = new URL("/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","/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 = "/webservices/qqOnlineWebService.asmx?wsdl";//:17001/DefDispatcher/dispatche?wsdlClient 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());}}以上代码直接复制到MyEclipse可直接调用,对于调用地址引用了天气预报和QQ的WS服务,希望对刚接触WS的同仁有帮助。

相关主题