如何在java中打印SOAP Header

时间:2015-12-10 10:39:41

标签: java soap

我可以知道如何使用java中的syso打印soap标头

这是我的示例代码:

       Envelope soapEnvelope = cesSOAPRequestBuilder.getSOAPEnvelope();
    URL url = new URL(getCesUrl());
    Message soapMessage = new Message();
    SOAPHTTPConnection connection = new SOAPHTTPConnection();
    connection.setTimeout(Integer.parseInt(EZSPProperties.getProperty("sp.commonProxy.defaultTimeout")));
    soapMessage.setSOAPTransport(connection);

    soapMessage.send(url, "", soapEnvelope);
    SOAPTransport soapTransport = soapMessage.getSOAPTransport();
    BufferedReader brResponse = soapTransport.receive();


       public Envelope getSOAPEnvelope() throws Exception{      
    //TODO when we start using WAS 5.1 and J2EE 1.4 perhaps it would be cleaner to replace all of this logic
    //with javax.xml.soap classes....see the Chapter 13 (SAAJ) http://java.sun.com/webservices/docs/1.3/tutorial/doc/index.html
    DOMImplementation dom = DOMImplementationImpl.getDOMImplementation();
    Document doc = dom.createDocument("doc","doc",null); 

    Vector soapHeaderEntries = new Vector();                                 

    Element messageInformationElement = doc.createElement("MessageInformation");
    soapHeaderEntries.add(messageInformationElement);   
    Element CESInformationElement = doc.createElement("CESInformation");
    soapHeaderEntries.add(CESInformationElement);       

    Element nextElement = doc.createElement("UniquePayloadId");
    Text txt = doc.createTextNode(uniquePayloadId);
    nextElement.appendChild(txt);             
    messageInformationElement.appendChild(nextElement); 

    nextElement = doc.createElement("MetName");
    txt = doc.createTextNode(metName);
    nextElement.appendChild(txt);    
    messageInformationElement.appendChild(nextElement);

    nextElement = doc.createElement("UserId");
    txt = doc.createTextNode(userId);
    nextElement.appendChild(txt);    
    CESInformationElement.appendChild(nextElement);

    nextElement = doc.createElement("ClientSystem");
    txt = doc.createTextNode(clientSystem);
    nextElement.appendChild(txt);    
    CESInformationElement.appendChild(nextElement);

    if (!"".equals(transactionMode)) {
        nextElement = doc.createElement("TransactionMode");
        txt = doc.createTextNode(transactionMode);
        nextElement.appendChild(txt);    
        CESInformationElement.appendChild(nextElement);
    }

    if (!"".equals (subCtxIdentifier)) {
        nextElement = doc.createElement("SubscriberContextIdentifier");
        txt = doc.createTextNode(subCtxIdentifier);
        nextElement.appendChild(txt);    
        CESInformationElement.appendChild(nextElement);
    }

    Header soapHeader = new Header();
    soapHeader.setHeaderEntries(soapHeaderEntries);

    Vector soapBodyEntries = new Vector();
    //in a document style SOAP service, the namespace of the first child element within the SOAP body points to the
    //name of the service (CEService) and the name of the first child element is the method that is executed within
    //the service(e.g. updateSubscriber). See "Writing Message Clients" within the SOAP User's guide at ws.apache.org    
    nextElement = doc.createElementNS("CEService", "m:"+service);       

    StringReader sr = new StringReader(enrollmentString);
    InputSource is = new InputSource(sr); 
    DOMParser domParser = new DOMParser();
    domParser.parse(is);                     
    Document enrollmentDoc = domParser.getDocument(); 
    Node importedNode = doc.importNode(enrollmentDoc.getFirstChild(),true);     
    nextElement.appendChild(importedNode);   

    soapBodyEntries.add(nextElement);

    Body soapBody = new Body();
    soapBody.setBodyEntries(soapBodyEntries); 

    Envelope envelope = new Envelope();      
    envelope.setHeader(soapHeader);
    envelope.setBody(soapBody); 

    return envelope; 
}

任何人都可以帮我打印标题信息以及正文。

1 个答案:

答案 0 :(得分:0)

SOAPTransport有getResponseSOAPContext()方法,返回SOAPContext object。你应该可以从那里继续。

SOAPContext ctx = soapTransport.getResponseSOAPContext();
java.util.Enumeration en = ctx.getRootPart().getAllHeaders();
//iterate and display the headers