Java实现不同的WebService 调用方式

时间:2023-03-09 02:54:30
Java实现不同的WebService 调用方式

请求过程分析:

  1.使用get方式获取wsdl文件,称为握手
  2.使用post发出请求
  3.服务器响应成功

  Java实现不同的WebService 调用方式

几种监听工具:
  http watch
  Web Service explorer
  eclipse 自带工具 TCP/IP Monitor

一.客户端调用(wximport自动生成代码 【推荐】)  

 public class App {
/**
* 通过wsimport 解析wsdl生成客户端代码调用WebService服务
* @param args
*/
public static void main(String[] args) {
/**
* <service name="MyService">
* 获得服务名称
*/
MyService mywebService = new MyService();
/**
* <port name="HelloServicePort" binding="tns:HelloServicePortBinding">
*/
HelloService hs = mywebService.getHelloServicePort();
/**
* 调用方法
*/
System.out.println(hs.sayGoodbye("sjk"));
System.out.println(hs.aliassayHello("sjk"));
}
}

二.通过ajax+js+xml调用

 <html>
<head>
<title>通过ajax调用WebService服务</title>
<script>
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
function sendMsg(){
var name = document.getElementById('name').value;
//服务的地址
var wsUrl = 'http://192.168.1.100:6789/hello';
//请求体
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">' +
' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>';
//打开连接
xhr.open('POST',wsUrl,true);
//重新设置请求头
xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
//设置回调函数
xhr.onreadystatechange = _back;
//发送请求
xhr.send(soap);
} function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
//alert('调用Webservice成功了');
var ret = xhr.responseXML;
var msg = ret.getElementsByTagName('return')[0];
document.getElementById('showInfo').innerHTML = msg.text;
//alert(msg.text);
}
}
}
</script>
</head>
<body>
<input type="button" value="发送SOAP请求" onclick="sendMsg();">
<input type="text" id="name">
<div id="showInfo">
</div>
</body>
</html>

三.URL Connection方式

 import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 通过UrlConnection调用Webservice服务
*
*/
public class App {
public static String getDataByConnectUrl(String ssoUrl){
try {
URL url = new URL(ssoUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setInstanceFollowRedirects(false);
con.setUseCaches(false);
con.setAllowUserInteraction(false);
con.connect();
StringBuffer sb = new StringBuffer();
String line = "";
BufferedReader URLinput = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
while((line=URLinput.readLine()) != null){
sb.append(line);
}
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return sb.toString();
}
}

四.单编程方式(和第一种方式一样)

 import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
class App{
private void incomeService() {
      String xmlStr = "xmlData";//xml格式的参数数据拼成的字符串
      String url = "http://127.0.0.1:8080/webProject/services/systemDDLService";/wsdl地址
      String method="incomeJC";
      String webObject = null;
      try {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference(url);
        options.setTo(targetEPR);
        QName opAddEntry = new QName("http://org.tempuri",method);
        Object[] opAddEntryArgs = new Object[] {xmlStr};
        Class[] classes = new Class[] { String.class };
        webObject = (String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
        System.out.println(webObject);
      }catch (Exception e) {
        e.printStackTrace();
        long end = System.currentTimeMillis();
      }
    }
}

原文:http://www.cnblogs.com/siqi/archive/2013/12/15/3475222.html