使用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

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。