2016-07-15 3 views
0

Я пытаюсь взаимодействовать с SOAP-сервисом. Я могу получить ответ SOAP и с помощью Source sourceContent = soapResponse.getSOAPPart().getContent(); и transformer.transform(sourceContent, result);, я могу видеть, что такое вывод/ответ и отображать его в консоли.Разбор SOAP-ответа и извлечение определенного узла с помощью JAVA

Но, мне нужно извлечь sessionID из ответа и отправить его sessionID в другом запросе SOAP.

Просьба предложить мне метод извлечения, создание нового запроса SOAP

Синтаксических, что мне нужно делать !!

Ниже приведен код для отправки запроса в службу SOAP:

public static void main(String args[]) { 
    try { 
     // Create SOAP Connection 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

     // Send SOAP Message to SOAP Server 
     String url = "https://WWW.DUMMYURL.COM"; 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 


     // Process the SOAP Response 
     printSOAPResponse(soapResponse); 

     soapConnection.close(); 
    } 

    catch (Exception e) { 
     System.err.println("Error occurred while sending SOAP Request to Server"); 
     e.printStackTrace(); 
    } 
} 

    private static SOAPMessage createSOAPRequest() throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 
     String serverURN = "urn:DUMMYURL.COM"; 
     String serverNS0 = "http://WWW.DUMMYURL.COM"; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     envelope.addNamespaceDeclaration("urn", serverURN); 
     envelope.addNamespaceDeclaration("ns0", serverNS0); 

     // SOAP Body 
     SOAPBody soapBody = envelope.getBody();  
     SOAPElement soapBodyElem1 = soapBody.addChildElement("login","urn"); 
     SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("username","urn"); 
     @SuppressWarnings("unused") 
     SOAPElement soapBodyElem3 = soapBodyElem2.addTextNode("USERNAME"); 
     SOAPElement soapBodyElem4 = soapBodyElem1.addChildElement("password","urn"); 
     @SuppressWarnings("unused") 
     SOAPElement soapBodyElem5 = soapBodyElem4.addTextNode("PASSWORD"); 
     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", "https://WWW.DUMMYURL.COM" + "login"); 
     soapMessage.saveChanges(); 

     //Print the request message 
     System.out.print("Request SOAP Message = "); 
     soapMessage.writeTo(System.out); 
     System.out.println(); 
     return soapMessage; 
} 


    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 

     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     String source = sourceContent.toString(); 
     System.out.print("\nResponse SOAP Message = "); 

     // Format it 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 

     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
     System.out.println(transformer.toString()); 
} 

Может кто-то пожалуйста, предложите мне фрагмент на как я должен сохранить ответ я получаю в файл локально?

В настоящее время в приведенном выше коде отображается response.

ответ

1

Вы можете использовать writeTo метод интерфейса SOAPMessage сделать то же самое, например:

FileOutputStream out = new FileOutputStream("somefile"); 
soapResponse.writeTo(out); 

Винод.

Смежные вопросы