1.使用ajax调用
var xhr; function invoke(){ if(window.ActiveXObject){ xhr = new ActiveXObject("Microsoft.XMLHTTP"); }else{ xhr = new XMLHttpRequest(); } //指定请求地址 var url = "http://127.0.0.1:7777/hello?wsdl"; //定义请求类型和地址和异步 xhr.open("POST", url, true); //设置Content-Type xhr.setRequestHeader("Content-Type", "text/xml;charset=UTF-8"); //指定回调方法 xhr.onreadystatechange = back; var textVal = document.getElementById("mytext").value; //组装消息体的数据 var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://server.hm.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +'<soapenv:Body>' +'<q0:sayHello>' +'<arg0>'+textVal+'</arg0>' +'</q0:sayHello>' +'</soapenv:Body>' +'</soapenv:Envelope>'; xhr.send(data); } function back(){ if(xhr.readyState == 4){ if(xhr.status == 200){ var doc = xhr.responseXML; alert(doc); alert(xhr.responseText); var tag = doc.getElementsByTagName("return")[0]; alert(tag) } } }2.通过URLConnection调用
//创建url地址 URL url = new URL("http://192.168.1.104:8080/hello"); //打开连接 URLConnection conn = url.openConnection(); //转换成HttpURL HttpURLConnection httpConn = (HttpURLConnection) conn; //打开输入输出的开关 httpConn.setDoInput(true); httpConn.setDoOutput(true); //设置请求方式 httpConn.setRequestMethod("POST"); //设置请求的头信息 httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8"); //拼接请求消息 String data = "<soapenv:Envelope xmlns:soapenv=" + "\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:q0=\"http://server.rl.com/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +"<soapenv:Body>" +"<q0:sayHello>" +"<arg0>renliang</arg0> " +"</q0:sayHello>" +"</soapenv:Body>" +"</soapenv:Envelope>"; //获得输出流 OutputStream out = httpConn.getOutputStream(); //发送数据 out.write(data.getBytes()); //判断请求成功 if(httpConn.getResponseCode() == 200){ //获得输入流 InputStream in = httpConn.getInputStream(); //使用输入流的缓冲区 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String line = null; //读取输入流 while((line = reader.readLine()) != null){ sb.append(line); } //创建sax的读取器 SAXReader saxReader = new SAXReader(); //创建文档对象 Document doc = saxReader.read(new StringReader(sb.toString())); //获得请求响应return元素 List<Element> eles = doc.selectNodes("//return"); for(Element ele : eles){ System.out.println(ele.getText()); } }3、使用jquery调用cxf
$(function(){ $("#mybutton").click(function(){ var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://server.web.cxf.rl.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +'<soapenv:Body>' +'<q0:sayHello>' +' <arg0>sss</arg0>' +' </q0:sayHello>' +'</soapenv:Body>' +'</soapenv:Envelope>'; $.ajax({ url:'http://localhost:8080/cxf-web-server/services/hello', type:'post', dataType:'xml', contentType:'text/xml;charset=UTF-8', data:data, success:function(responseText){ alert($(responseText).find('return').text()); }, error:function(){ alert("error"); } }) }) })