加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

十二、用Axis操作 Header头部信息

发布时间:2020-12-17 01:23:30 所属栏目:安全 来源:网络整理
导读:Axis中操作Header的信息是通过Handler来完成的,在读取Header的信息的时候,可以完成Header的操作的Handler有 JWSHandler、SOAPMonitorHandler、JAXRPCHandler。JWSHandler是完成jws发布模式的 WebService的Header的读取操作, SOAPMonitorHandler是完成SOAP

Axis中操作Header的信息是通过Handler来完成的,在读取Header的信息的时候,可以完成Header的操作的Handler有 JWSHandler、SOAPMonitorHandler、JAXRPCHandler。JWSHandler是完成jws发布模式的 WebService的Header的读取操作,

SOAPMonitorHandler是完成SOAPMonitor模式的,JAXRPCHandler是java rpc模式的。

下面将会用JAXRPCHandler读取客户端的Header信息,然后用BasicHandler将客户端请求的Header写入客户端的响应的Header中。

1、 首先编写一个方法,这个方法什么也不做。就是在这个方法请求和响应的时候分别完成读取Header的信息和写入响应的Header内容。代码如下:?

? ?

?

?

2、 客户端的Header中的内容是下面这样的xml内容

? ?

?

?

下面将用客户端代码将上面的xml元素添加的Header中,代码如下:

?

?

上面的就完成了xml中的内容,并且添加到请求的Header中。

3、 根据上面的Header的内容,我们现在编写一个Handler用来读取客户端请求信息中的Header的内容,这个Handler需要继承JAXRPCHandler。代码如下:

?

?

?

上面读取Header内容的代码有点小长,不过比较简单。就是用循环遍历Header的内容,Header的内容是一个xml的文档。所以用循环一步步向里面遍历,不过感觉写个递归会更简单。

4、 下面将编写一个Handler来向客户端的Response中写入Header内容,写入的Header的内容就是客户端请求的Header内容。代码如下:

? ?

?

?

上面的代码是取出respHeader = (SOAPHeader) respSe.getHeader();

然后循环遍历Header的元素写入到respHeader.addChildElement(she);

5、 好了,上面已经写好了WebService的方法,以及请求时读取Header内容的Handler和在响应时写入Header内容的Handler。下面开始写wsdd发布当前WebService。

?

?

还是用doc命令发布当前WebService,命令如下:

C:/SoftWare/tomcat-5.0.28/tomcat-5.0.28/webapps/AxisWebService/WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/AxisWebService/services/AdminService deployHeader.wsdd

发布完成后,你会看的

Processing file deployHeader.wsdd

<Admin>Done processing</Admin>

的字样就表示发布成功了,然后在浏览器地址栏输入:

http://localhost:8080/AxisWebService/servlet/AxisServlet

你就可以看到OperaterHandler这个WebService了

6、 OperaterHandler这个WebService成功发布,现在就是要编写客户端代码调用这个WebService,客户端代码比较简单,完整代码如下:

?

?

?

?

call.getResponseMessage();可以得到服务器响应的Header的信息

运行后你可以看到客户端控制台会输出:

?

<ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ServiceName xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="xsd:string" xmlns:ns2="service" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">1.0</ns2:Version><author><name xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">axis</name><age xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">14</age></author><copyRight><year xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010</year><number xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">123123123</number></copyRight><Tel xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">555666442</Tel></ns1:Header>

responseMessage:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>

responseMessage前面的内容是客户端将会写入到Header中的xml内容,而responseMessage后面的内容就是响应的Header的内容,这段内容是WriteHeaderHandler写入到ResponseMessage中的。

再看看服务器端控制台的内容:

父节点:Header,节点:ServiceName,值:ReadHeaderService

父节点:Header,节点:CreateDate,值:2010-12-19T06:22:59.931Z

父节点:Header,节点:Version,值:1.0

父父节点:Header,父节点:author,节点:name##name,值:axis##axis

父父节点:Header,节点:age##age,值:14##14

父父节点:Header,父节点:copyRight,节点:year##year,值:2010##2010

父父节点:Header,节点:number##number,值:123123123##123123123

父节点:Header,节点:Tel,值:555666442

ns1:Header

header Info:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>

上面的文本内容是ReadHeaderHandler读取客户端的Header的内容和我们定制的xml的内容是一致的。而后面的xml能入是WriteHeaderHandler中输出的内容。

作者:hoojo
出处: http://www.cnblogs.com/hoojo/archive/2010/12/20/1911396.html
blog:http://blog.csdn.net/IBM_hoojo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读