JAva呼び出しWebService

15868 ワード

一、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接続で呼び出す

 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   ,   OutputStreamWriter
13             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 response
20             reader = new BufferedReader(new InputStreamReader(conn
21                     .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 block
29             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 block
40                 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_第1张图片