請求過程分析:
1.使用get方式獲取wsdl文件,稱為握手
2.使用post發出請求
3.服務器響應成功
幾種監聽工具:
http watch
Web Service explorer
eclipse 自帶工具 TCP/IP Monitor
一.客戶端調用(wximport自動生成代碼 【推薦】)

1 public class App { 2 /** 3 * 通過wsimport 解析wsdl生成客戶端代碼調用WebService服務 4 * @param args 5 */ 6 public static void main(String[] args) { 7 /** 8 * <service name="MyService"> 9 * 獲得服務名稱 10 */ 11 MyService mywebService = new MyService(); 12 /** 13 * <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> 14 */ 15 HelloService hs = mywebService.getHelloServicePort(); 16 /** 17 * 調用方法 18 */ 19 System.out.println(hs.sayGoodbye("sjk")); 20 System.out.println(hs.aliassayHello("sjk")); 21 } 22 }
二.通過ajax+js+xml調用

1 <html> 2 <head> 3 <title>通過ajax調用WebService服務</title> 4 <script> 5 var xhr = new ActiveXObject("Microsoft.XMLHTTP"); 6 function sendMsg(){ 7 var name = document.getElementById('name').value; 8 //服務的地址 9 var wsUrl = 'http://192.168.1.100:6789/hello'; 10 //請求體 11 var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + 12 ' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>'; 13 //打開連接 14 xhr.open('POST',wsUrl,true); 15 //重新設置請求頭 16 xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8"); 17 //設置回調函數 18 xhr.onreadystatechange = _back; 19 //發送請求 20 xhr.send(soap); 21 } 22 23 function _back(){ 24 if(xhr.readyState == 4){ 25 if(xhr.status == 200){ 26 //alert('調用Webservice成功了'); 27 var ret = xhr.responseXML; 28 var msg = ret.getElementsByTagName('return')[0]; 29 document.getElementById('showInfo').innerHTML = msg.text; 30 //alert(msg.text); 31 } 32 } 33 } 34 </script> 35 </head> 36 <body> 37 <input type="button" value="發送SOAP請求" onclick="sendMsg();"> 38 <input type="text" id="name"> 39 <div id="showInfo"> 40 </div> 41 </body> 42 </html>
三.URL Connection方式

1 import java.io.InputStream; 2 import java.io.OutputStream; 3 import java.net.HttpURLConnection; 4 import java.net.URL; 5 /** 6 * 通過UrlConnection調用Webservice服務 7 * 8 */ 9 public class App { 10 public static String getDataByConnectUrl(String ssoUrl){ 11 try { 12 URL url = new URL(ssoUrl); 13 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 14 con.setInstanceFollowRedirects(false); 15 con.setUseCaches(false); 16 con.setAllowUserInteraction(false); 17 con.connect(); 18 StringBuffer sb = new StringBuffer(); 19 String line = ""; 20 BufferedReader URLinput = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8")); 21 while((line=URLinput.readLine()) != null){ 22 sb.append(line); 23 } 24 con.disconnect(); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 return null; 28 } 29 return sb.toString(); 30 } 31 }
四.單編程方式(和第一種方式一樣)

1 import javax.xml.namespace.QName; 2 import org.apache.axis2.addressing.EndpointReference; 3 import org.apache.axis2.client.Options; 4 import org.apache.axis2.rpc.client.RPCServiceClient; 5 class App{ 6 private void incomeService() { 7 String xmlStr = "xmlData";//xml格式的參數數據拼成的字符串 8 String url = "http://127.0.0.1:8080/webProject/services/systemDDLService";/wsdl地址 9 String method="incomeJC"; 10 String webObject = null; 11 try { 12 RPCServiceClient serviceClient = new RPCServiceClient(); 13 Options options = serviceClient.getOptions(); 14 EndpointReference targetEPR = new EndpointReference(url); 15 options.setTo(targetEPR); 16 QName opAddEntry = new QName("http://org.tempuri",method); 17 Object[] opAddEntryArgs = new Object[] {xmlStr}; 18 Class[] classes = new Class[] { String.class }; 19 webObject = (String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]; 20 System.out.println(webObject); 21 }catch (Exception e) { 22 e.printStackTrace(); 23 long end = System.currentTimeMillis(); 24 } 25 } 26 }
原文:http://www.cnblogs.com/siqi/archive/2013/12/15/3475222.html