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

简介:

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, 父节点:author, 节点:age##age, 值:14##14

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

父父节点:Header, 父节点:copyRight, 节点: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,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
缓存 自然语言处理
常见header 字段解释
常见header 字段解释
|
8月前
G2Plot Tooltip 自定义头部、尾部、辅助线
G2Plot Tooltip 自定义头部、尾部、辅助线
128 0
|
PHP 调度 数据安全/隐私保护
【源码解读】TP5读取本地图片输出后,设置header头无效,图片乱码
在Thinkphp程序中读取本地图片,做出加工处理(如合并二维码等水印),然后输出给客户端,一直输出图片内容乱码。 设置了header image/png 不生效。 写下这篇TP源码排查文章,看看问题到底出现在哪个步骤。
432 0
【源码解读】TP5读取本地图片输出后,设置header头无效,图片乱码
页面中的位置:client、page、screen、offset、以及元素视图位置的区别和方法
页面中的位置:client、page、screen、offset、以及元素视图位置的区别和方法
|
7月前
|
安全 JavaScript 前端开发
目标网站已经设置了一个"X-Frame-Options"头
目标网站已经设置了一个"X-Frame-Options"头
66 2
|
缓存 自然语言处理
常见Header字段解释
大家好,我是阿萨。 周一来了,又是元气满满的一天。 看完《独行月球》轻松幽默喜剧,感觉整个人都是轻松的。不过今天的话题可不是这部喜剧篇。依旧是枯燥乏味的技术文。
157 0
|
PHP
php常用的header头部定义
php常用的header头部定义
80 0
|
存储 关系型数据库 数据库
Data dictionary header(2) --系统表空间结构(三十四)
Data dictionary header(2) --系统表空间结构(三十四)
SwiftUI—如何调整记录在List列表里的顺序
SwiftUI—如何调整记录在List列表里的顺序
204 0
SwiftUI—如何调整记录在List列表里的顺序
SwiftUI—如何往一个List列表里插入新的记录
SwiftUI—如何往一个List列表里插入新的记录
286 0
SwiftUI—如何往一个List列表里插入新的记录