java 調用 WebService

來源:互聯網
上載者:User

標籤:

一、wximport自動產生代碼

wsimport -keep -p com.test.client http://localhost:8080/test/services/TestService?wsdl

 

-d:產生用戶端執行類的class檔案的存放目錄
-s:產生用戶端執行類的源檔案的存放目錄
-p:定義產生類的包名

 

二、通過ajax調用(不支援跨域調用)

 1 function callAxisWsPost(method, variable, value, url, _Namespace, callback, loadProcess) { 2                 function getlen(str) { 3                     var bytesCount = 0; 4                     for (var i = 0; i < str.length; i++) { 5                         var c = str.charAt(i); 6                         if (/^[\u0000-\u00ff]$/.test(c))   //匹配雙位元組 7                         { 8                             bytesCount += 1; 9                         }10                         else {11                             bytesCount += 2;12                         }13                     }14                     return bytesCount;15                 }16 17                 var xmlhttp = null;18                 if (window.ActiveXObject) {19                     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");20                 } else if (window.XMLHttpRequest) {21                     xmlhttp = new XMLHttpRequest();22                 }23                 var data;24                 data = ‘<?xml version="1.0" encoding="utf-8"?>‘;25                 data = data + ‘<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/">‘;26                 data = data + ‘<soap:Body>‘;27                 data = data + ‘<‘ + method + ‘ xmlns="‘ + _Namespace + ‘">‘;28                 for (var i = 0; i < variable.length; i++) {29                     data = data + ‘<‘ + variable[i] + ‘>‘ + value[i] + ‘</‘ + variable[i] + ‘>‘;30 31                 }32                 data = data + ‘</‘ + method + ‘>‘;33                 data = data + ‘</soap:Body>‘;34                 data = data + ‘</soap:Envelope>‘;35                 xmlhttp.onreadystatechange = function () {36                     if (xmlhttp.readyState == 1) {37                         if (loadProcess)38                             loadProcess();39                     }40                     if (xmlhttp.readyState == 4) {41                         if (xmlhttp.status == 200) {42                             if (callback)43                                callback(xmlhttp.responseText);44                         }45                     }46                 }47 48                 xmlhttp.Open("POST", url, true);49                 xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=utf-8");50                 xmlhttp.SetRequestHeader("Content-Length", getlen(data));51                 xmlhttp.SetRequestHeader("SOAPAction", _Namespace + method);52                 xmlhttp.Send(data);53             }
View Code

 

三、通過URL Connection調用

 1 private  StringBuffer urlConnectionPost(String tourl,StringBuffer data) { 2         StringBuffer sb = null; 3         BufferedReader reader = null; 4         OutputStreamWriter wr = null; 5         URL url; 6         try { 7             url = new URL(tourl); 8             URLConnection conn = url.openConnection(); 9             conn.setDoOutput(true);10             conn.setConnectTimeout(1000 * 5);11             12             //當存在post的值時,才開啟OutputStreamWriter13             if(data!=null && data.toString().trim().length()>0){14                 wr = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");15                 wr.write(data.toString());16                 wr.flush();17             }18            19             // Get the response20             reader = new BufferedReader(new InputStreamReader(conn21                     .getInputStream(),"UTF-8"));22             sb = new StringBuffer();23             String line = null;24             while ((line = reader.readLine()) != null) {25                 sb.append(line + "/n");26             }27         } catch (IOException e) {28             // TODO Auto-generated catch block29             e.printStackTrace();30         }finally{31             try {32                 if(wr!=null){33                     wr.close();34                 }35                 if(reader!=null){36                     reader.close();37                 }38             } catch (IOException e) {39                 // TODO Auto-generated catch block40                 e.printStackTrace();41             }            42         }43         return sb;44     }
View Code

四、通過HttpClient調用                          

轉載:http://blog.csdn.net/bhq2010/article/details/9210007

 

基於瀏覽器的測試載入器(RESTClient)

 組件附加地址:https://addons.mozilla.org/zh-CN/firefox/addon/restclient/

 

java 調用 WebService

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.