使用AXIS调用需要身份验证的WebService

使用AXIS调用WebService的方法请参见我另一篇博文Java调用WebService的方法总结,这里只记叙使用axis验证身份。

1、验证信息通过请求报文头传递

  我遇到的情况是访问WebService需要进行Windows身份验证,做法是将用户名和密码信息加密后通过添加“Authorization”请求报文头发送,所以实现的Java代码如下:

/*
* Create new Service and Call objects. These are the standard
* JAX-RPC objects that are used to store metadata about the service
* to invoke.
*/
Service service = new Service();
Call call = (Call) service.createCall();

.......      
Hashtable
<String,String> headers = (Hashtable<String,String>) call.getProperty(HTTPConstants.REQUEST_HEADERS); if (headers == null) { headers = new Hashtable<String, String>(); call.setProperty(HTTPConstants.REQUEST_HEADERS, headers); } /* Put your header into the headers Hashtable */ headers.put("Authorization", "Basic MDMwNDExMTUwOjEyMzQ1Ng==");

监听HTTP请求可看到如下报文头:

POST /sap/bc/srt/rfc/sap/zwf_xmas/300/service HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: hdeccd.evergrande.com:8000
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 573
Authorization: Basic MDMwNDExMTUwOjEyMzQ1Ng==

 

参考资料:http://www.jroller.com/cwap/entry/setting_http_headers_in_axis1

2、验证信息写入SOAP报文中


Java代码:

      try {
                String namespace = "";
                SOAPHeaderElement header = new SOAPHeaderElement(namespace, "Authentication");
                header.setPrefix("");
                header.addChildElement("Username").addTextNode("030411150");
                header.addChildElement("Password").addTextNode("123456");
                call.addHeader(header);
            } catch (SOAPException e1) {
                e1.printStackTrace();
            }

监听HTTP请求可看到如下报文信息:

<?xml version="1.0" encoding="UTF-8"?>

<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:Header>
    <
Authentication soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="">
      <
Username>030411150</Username>
      <
Password>123456</Password>
    </
Authentication>
  </
soapenv:Header>
  <
soapenv:Body>
    <
ns1:ZdefmWfMoveGetbusinesstable soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:sap-com:document:sap:soap:functions:mc-style">
      <
ITabNo xsi:type="xsd:string">1</ITabNo>
      <
IWfnum xsi:type="xsd:string">1</IWfnum>
      <
IWiId xsi:type="xsd:string">1</IWiId>
    </
ns1:ZdefmWfMoveGetbusinesstable>
  </
soapenv:Body>
</
soapenv:Envelope>

参考资料:http://shenxueliang.iteye.com/blog/1734414

  

转载于:https://www.cnblogs.com/dragon-aslan/p/4159392.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值