JAVA调用WebService总结

一、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的值时,才打开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

五、通过Axis调用

public String callUrl(String xml) throws Exception {
        String url = "http://10.190.111.123:8080/TestService/services/TEST_SERVICE?wsdl";
        String method = "testQuery";
        
        return callUrl(url,method,xml);
    }
    
    public String callUrl(String url,String method,String xml) throws Exception {
        Object returnInfo = null;
        Service service;
        try {
            service = new Service();
            Call call = (Call)service.createCall();        
            call.setTargetEndpointAddress(new URL(url));
            call.setOperationName(method);
            returnInfo = call.invoke(new Object[] {
                    xml
            });
        } catch(Exception e) {
            logger.error(e.getMessage(), e.fillInStackTrace());
        }
        
        if(returnInfo == null) {
            return null;
        } else {
            return returnInfo.toString();
        }
    }

开源测试工具(Soapui)

  组件地址:http://www.soapui.org/

基于浏览器的测试工具(RESTClient)

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

原文地址:https://www.cnblogs.com/Nadim/p/4648145.html