AXIS报文头SOAPHEADER认证

 public class MyClient
{
    //后台接口类
    private MyStub stub;
   
    public MyClient (){
       
    }
    /**
     * constructor with spec url
     */
    public MyClient (String url)
        throws Exception
    {
        URL u = new URL(url);
        Service service = new Service();
        stub = new MyStub(u, service);
        SOAPHeaderElement ppElement = setSoapHeader();
        // add headerEle
        stub.setHeader(ppElement);
    }
   
    /**
     * constructor with spec url and service
     */
    public MyClient(URL url, Service service)
        throws Exception
    {
        stub = new MyStub(url, service);
        SOAPHeaderElement ppElement = setSoapHeader();
        // add headerEle
        stub.setHeader(ppElement);
    }
        
    
    /**
     *
     * 设置头信息SOAPHeader验证
     * @return
     * @throws ParserConfigurationException
     * @see [类、类#方法、类#成员]
     */
    private SOAPHeaderElement setSoapHeader() throws ParserConfigurationException
    {
        // create document of w3c
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();
        // create Element
        Element security = document.createElementNS(" namespace", "Security");
        Element usernameToken = document.createElementNS(" namespace", "UsernameToken");
        Element username = document.createElementNS(" namespace", "Username");
        Element password = document.createElementNS(" namespace", "Password");
       
        // add Element
        usernameToken.appendChild(username);
        usernameToken.appendChild(password);
        security.appendChild(usernameToken);
       
        // set value
        Text usernameValue = document.createTextNode("test");
        username.appendChild(usernameValue);
        Text passwordValue = document.createTextNode("2121");
        password.appendChild(passwordValue);
       
        // create SOAPHeaderElement
        SOAPHeaderElement ppElement = new SOAPHeaderElement(security);
        return ppElement;
    }
 
    /**
     * @return 返回 stub
     */
    public MyStub getStub()
    {
        return stub;
    }
    /**
     * @param 对stub进行赋值
     */
    public void setStub(MyStub stub)
    {
        this.stub = stub;
    }
}

你可能感兴趣的:(SOAP,WebService,AXIS)