使用AXIOM给WebService调用增加SOAP头

 

 直接上代码

public static void employeeTest(){
  try {
    HBWebServiceStub stub = new HBWebServiceStub();
    ServiceClient sc = stub._getServiceClient();
//    sc.addStringHeader(new QName("http://axis2.xxxx.net", "UserName", "ns1"), "testuser");
//    sc.addStringHeader(new QName("http://axis2.xxxx.net", "PassWord", "ns1"), "111111");		
    OMFactory omFactory = OMAbstractFactory.getOMFactory();
//    OMElement omElement = omFactory.createOMElement(new QName("http://axis2.xxxx.net", "ERPSoapHeader", "ns1"), null);
    OMNamespace omNs = omFactory.createOMNamespace("http://axis2.xxxx.net", "ns1");

    OMElement header = omFactory.createOMElement("ERPSoapHeader", omNs);

    OMElement un = omFactory.createOMElement("UserName", omNs);
    un.addChild(omFactory.createOMText(un, "testuser"));
    header.addChild(un);
    OMElement pw = omFactory.createOMElement("PassWord", omNs);
    pw.addChild(omFactory.createOMText(pw, "111111"));
    header.addChild(pw);

    sc.addHeader(header);
			
    DataSync dataSync = new DataSync();
    dataSync.setStrXML(strXml);
    DataSyncResponse resp = stub.dataSync(dataSync);
    System.out.println("resp: " + resp.get_return());
  } catch (AxisFault e) {
    e.printStackTrace();
  } catch (RemoteException e) {
    e.printStackTrace();
  } catch (HBWebServiceDocumentExceptionException e) {
    e.printStackTrace();
  }
}

 

你可能感兴趣的:(webservice)