webservice使用js访问的方式

选自javaee技术体系中的webservice技术


这一个个是上一篇文章webservice生成客户端的方式之一,为什么单独拿出来一章讲解,是因为这篇文章更接近webservice的底层。


服务端的方式不在讲解,通过使用:http://192.168.1.102:8089/test?wsdl能访问到wsdl文件。


介绍一个eclipse的工具:launch the webservices Explorer


主界面如下所示:



这是eclipse提供的一个webservice客户端浏览器,输入正确的wsdl文件,如:http://192.168.1.102:8089/test?wsdl,塔会解析,如下图所示:



重点分析请求与响应的过程:

webservie中的一个服务方法如下:

@WebMethod
	@Override
	public String fun(String name) {
		System.out.println("bbbbbbb");
		return name + "------->test";
	}

使用刚才的工具进行随便输入参数访问该方法得到的如下请求与响应内容如下:

请求内容:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://impl.webservice.lgy.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <q0:fun>
  <arg0>sxsx</arg0> 
  </q0:fun>
  </soapenv:Body>
  </soapenv:Envelope>


响应内容:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
- <S:Body>
- <ns2:funResponse xmlns:ns2="http://impl.webservice.lgy.com/">
  <return>sxsx------->test</return> 
  </ns2:funResponse>
  </S:Body>
  </S:Envelope>


介绍一个eclipse的工具:TCP/IP monitor

界面,设置说明如下:




说一下具体使用流程:

           此监听是一个工具,只是一个中间转换器,相当于你需要监听的端口转换成为你的写的端口,然后到你这里在转发到你需要监听的进程里面。此监听器可以监听到任何http的请求信息。




通过tcp/ip工具监听上面的接口

请求信息如下:

Header: POST /test?wsdl HTTP/1.0

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<fun xmlns="http://impl.webservice.lgy.com/">
<arg0 xmlns="">GGG</arg0>
</fun>
</soapenv:Body>
</soapenv:Envelope>



响应信息如下:

Header: HTTP/1.1 200 OK

<?xml version="1.0"  standalone="no"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:funResponse xmlns:ns2="http://impl.webservice.lgy.com/">
<return>GGG------->test</return>
</ns2:funResponse>
</S:Body>
</S:Envelope>

说明:webservice是一个http的post请求,交互信息载体是soap。



此时ajax发送请求信息如上即可:

<script type="text/javascript">
	$(function() {
		var url = "http://localhost:8089/test";
		var data = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'" 
		      		+ " xmlns:q0='http://impl.webservice.lgy.com/'" 
		           	+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" 
		            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"
		    	    + "<soapenv:Body><q0:fun><arg0>v</arg0></q0:fun></soapenv:Body>"
		            + "</soapenv:Envelope>";
		      
		var xmlhttp = getXmlHttpRequest();
        xmlhttp.open("POST",url,true);  
        xmlhttp.setRequestHeader("Content-Length",data.length);  
        xmlhttp.setRequestHeader("CONTENT-TYPE","text/xml;charset=UTF-8");  
        xmlhttp.onreadystatechange=function(){  
            if(xmlhttp.readyState == 4)  
            {     
                console.log(xmlhttp.responseText);  
            }  
        }  
        xmlhttp.send(data);  
	});

	function getXmlHttpRequest() {
		var xmlhttp;

		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xmlhttp;
	}
</script>

此时,浏览器端会得到响应:

<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:funResponse xmlns:ns2="http://impl.webservice.lgy.com/"><return>v------->test</return></ns2:funResponse></S:Body></S:Envelope>

ps:有时候会遇到浏览器跨域问题,你ajax请求的的地址跨域了(端口不一致也算,除非你用的ie)。请求跨域地址时,ajax先用option方法请求你的这个地址获取Access-Control-Allow-Origin响应头是否包含请求域名或者为*允许跨域,如果不允许会报跨域错误,你的控制台输出这个错误是因为你服务器无法处理option方法请求,自己找下java如何配置允许option请求。ie下面无此问题,火狐却有。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值