WebService学习(十三)——调用webservice的方式(其他方式)

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


上一篇:关于java请求webservice的方法


下一篇:LINQ之路18:LINQ to XML之导航和查询