java调用WebService(客户端)

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

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文档页,该文档页被传递到提出请求的客户端

对于SOAP协议多写了个CXF的调用(对于CXF必须有以下包:)

以下是调用代码

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.io.OutputStream;  
  6. import java.io.OutputStreamWriter;  
  7. import java.net.URL;  
  8. import java.net.URLConnection;  
  9. import java.net.URLEncoder;  
  10.   
  11. import org.apache.cxf.endpoint.Client;  
  12. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
  13.   
  14. /** 
  15.  * 功能描述:WebService调用 
  16.  *  
  17.  */  
  18. public class ClientTest {  
  19.   
  20.     /** 
  21.      * 功能描述:HTTP-GET 
  22.      *  
  23.      */  
  24.     public String get() {  
  25.         URL url;  
  26.         BufferedReader bin = null;  
  27.         StringBuilder result = new StringBuilder();  
  28.         try {  
  29.             String urlTemp = "http://www.webxml.com.cn//WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName="  
  30.                     + URLEncoder.encode("福建""UTF-8");// URLEncoder用来参数编码  
  31.             url = new URL(urlTemp);  
  32.             InputStream in = url.openStream(); // 请求  
  33.             bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));  
  34.             String s = null;  
  35.             while ((s = bin.readLine()) != null) {  
  36.                 result.append(s);  
  37.             }  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         } finally {  
  41.             if (null != bin) {  
  42.                 try {  
  43.                     bin.close();  
  44.                 } catch (IOException e) {  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.         }  
  49.         return result.toString();  
  50.     }  
  51.   
  52.     /** 
  53.      * 功能描述:HTTP-POST 
  54.      *  
  55.      */  
  56.     public String post() {  
  57.         OutputStreamWriter out = null;  
  58.         StringBuilder sTotalString = new StringBuilder();  
  59.         try {  
  60.             URL urlTemp = new URL(  
  61.                     "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity");  
  62.             URLConnection connection = urlTemp.openConnection();  
  63.             connection.setDoOutput(true);  
  64.             out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");  
  65.             StringBuffer sb = new StringBuffer();  
  66.             sb.append("byProvinceName=福建");  
  67.             out.write(sb.toString());  
  68.             out.flush();  
  69.             String sCurrentLine;  
  70.             sCurrentLine = "";  
  71.             InputStream l_urlStream;  
  72.             l_urlStream = connection.getInputStream();// 请求  
  73.             BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
  74.                     l_urlStream));  
  75.             while ((sCurrentLine = l_reader.readLine()) != null) {  
  76.                 sTotalString.append(sCurrentLine);  
  77.             }  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             if (null != out) {  
  82.                 try {  
  83.                     out.close();  
  84.                 } catch (IOException e) {  
  85.                     e.printStackTrace();  
  86.                 }  
  87.             }  
  88.         }  
  89.         return sTotalString.toString();  
  90.     }  
  91.   
  92.     /** 
  93.      * 功能描述: 请求 HTTP-SOAP 
  94.      *  
  95.      */  
  96.     public String getSoapInputStream() {  
  97.         try {  
  98.             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>";  
  99.             URL url = new URL(  
  100.                     "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");  
  101.             URLConnection conn = url.openConnection();  
  102.             conn.setUseCaches(false);  
  103.             conn.setDoInput(true);  
  104.             conn.setDoOutput(true);  
  105.   
  106.             conn.setRequestProperty("Content-Length", Integer.toString(soap  
  107.                     .length()));  
  108.             conn.setRequestProperty("Content-Type""text/xml; charset=utf-8");  
  109.             conn.setRequestProperty("SOAPAction",  
  110.                     "http://WebXml.com.cn/getSupportCity");  
  111.   
  112.             OutputStream os = conn.getOutputStream();  
  113.             OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");  
  114.             osw.write(soap);  
  115.             osw.flush();  
  116.             osw.close();  
  117.             StringBuilder sTotalString = new StringBuilder();  
  118.             String sCurrentLine = "";  
  119.             InputStream is = conn.getInputStream();  
  120.             BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
  121.                     is));  
  122.             while ((sCurrentLine = l_reader.readLine()) != null) {  
  123.                 sTotalString.append(sCurrentLine);  
  124.             }  
  125.             return sTotalString.toString();  
  126.         } catch (Exception e) {  
  127.             e.printStackTrace();  
  128.             return null;  
  129.         }  
  130.     }  
  131.   
  132.     /** 
  133.      * 功能描述:使用CXF 请求 HTTP-SOAP 
  134.      *  
  135.      */  
  136.     public String soap() {  
  137.         JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory  
  138.                 .newInstance();  
  139.         String url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";// http://www.fjyxd.com:17001/DefDispatcher/dispatche?wsdl  
  140.         Client clientTemp = clientFactory.createClient(url);  
  141.         Object[] arg;  
  142.         String result = "";  
  143.         try {  
  144.             arg = clientTemp.invoke("qqCheckOnline""8698053");// 查询8698053在线状态,QQ号码  
  145.             // String,默认QQ号码:8698053。返回数据:String,Y  
  146.             // = 在线;N = 离线;E  
  147.             // = QQ号码错误;A =  
  148.             // 商业用户验证失败;V =  
  149.             // 免费用户超过数量  
  150.             result = (String) arg[0];  
  151.         } catch (Exception e) {  
  152.             e.printStackTrace();  
  153.         }  
  154.         return result;  
  155.     }  
  156.   
  157.     /** 
  158.      * 功能描述:调用 
  159.      *  
  160.      */  
  161.     public static void main(String[] args) {  
  162.         ClientTest ct = new ClientTest();  
  163.         System.out.println("HTTP-GET结果:" + ct.get());  
  164.         System.out.println("HTTP-POST结果:" + ct.post());  
  165.         System.out.println("HTTP-SOAP结果:" + ct.getSoapInputStream());  
  166.         System.out.println("CXF HTTP-SOAP结果:" + ct.soap());  
  167.     }  
  168.   
  169. }  

转自:

https://blog.csdn.net/javaalpha/article/details/8488385

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值