2017-05-29 93 views
0

我想從SOAP URL的請求中獲得響應。對於這個我使用下面的代碼Java獲取來自SOAP服務的響應URL調用

import javax.xml.soap.MessageFactory; 
import javax.xml.soap.MimeHeaders; 
import javax.xml.soap.SOAPBody; 
import javax.xml.soap.SOAPConnection; 
import javax.xml.soap.SOAPConnectionFactory; 
import javax.xml.soap.SOAPElement; 
import javax.xml.soap.SOAPEnvelope; 
import javax.xml.soap.SOAPHeader; 
import javax.xml.soap.SOAPMessage; 
import javax.xml.soap.SOAPPart; 
import javax.xml.transform.Source; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 

public class Test { 
    final static String UserLogin = "[email protected]"; 

    public static void main(String[] args) { 
     try { 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 
      String url = "http://devlocal04:8080/arsys/WSDL/public/devlocal03/DAL:OrderShim_WS"; 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 
      printSOAPResponse(soapResponse); 
      soapConnection.close(); 
     } catch (Exception e) { 
      System.out.println("Exception : " + e); 
     } 
    } 

    private static SOAPMessage createSOAPRequest() throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 
     String serverURI = "urn:DAL:OrderShim_WS"; 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     envelope.addNamespaceDeclaration("urn", serverURI); 

     SOAPHeader soapHeader = envelope.getHeader(); 
     SOAPElement soapHeaderElem = soapHeader.addChildElement("AuthenticationInfo", "urn"); 
     SOAPElement soapHeaderElem1 = soapHeaderElem.addChildElement("userName", "urn"); 
     soapHeaderElem1.addTextNode("Test"); 
     SOAPElement soapHeaderElem2 = soapHeaderElem.addChildElement("password", "urn"); 
     soapHeaderElem2.addTextNode("12345"); 
     SOAPBody soapBody = envelope.getBody(); 
     SOAPElement soapBodyElem = soapBody.addChildElement("Get_Order", "urn"); 
     SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Login_Name", "urn"); 
     soapBodyElem1.addTextNode(UserLogin); 
     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", serverURI + "Login_Name"); 

     soapMessage.saveChanges(); 
     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(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
    } 
} 

我必須做的請求像下面

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DAL:OrderShim_WS"> 
    <soapenv:Header> 
     <urn:AuthenticationInfo> 
     <urn:userName>Test</urn:userName> 
     <urn:password>12345</urn:password> 
     </urn:AuthenticationInfo> 
    </soapenv:Header> 
    <soapenv:Body> 
     <urn:Get_Order> 
     <urn:Login_Name>[email protected]</urn:Login_Name> 
     </urn:Get_Order> 
    </soapenv:Body> 
</soapenv:Envelope> 

但是當我運行我的代碼,他們通過下文的請求

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DAL:OrderShim_WS"> 
    <SOAP-ENV:Header> 
     <urn:AuthenticationInfo> 
      <urn:userName>Test</urn:userName> 
      <urn:password>12345</urn:password> 
     </urn:AuthenticationInfo> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
     <urn:Get_Order> 
      <urn:Login_Name>[email protected]</urn:Login_Name> 
     </urn:Get_Order> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

在調用這個SOAP我的請求我可以在「soapenv」的位置看到「SOAP-ENV」,但我沒有得到有效的回覆。

我正在使用Java 1.8。

+0

SOAP-ENV是Spring-WS使用的默認前綴請參閱https://stackoverflow.com/questions/39242197/how-to-change-soap-env-default-prefix-of-spring-ws – sercasti

回答

1

我認爲你正在因爲SOAP-ENV.Replace SOAP-ENV的這個錯誤與soapenv或提出請求相同required.You會得到一個想法從我的代碼

MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 

     String serverURI = ""; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     soapMessage 
       .getSOAPPart() 
       .getEnvelope() 
       .removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", 
         "SOAP-ENV"); 
     soapMessage.getSOAPPart().getEnvelope() 
       .removeAttribute("xmlns:SOAP-ENV"); 
     soapMessage.getSOAPHeader().detachNode(); 
     soapMessage.getSOAPPart().getEnvelope().setPrefix("soap"); 
     soapMessage.getSOAPBody().setPrefix("soap"); 

// envelope.addNamespaceDeclaration("ns1", serverURI); 
// we need to send the soap request in the format mentioned below 
/* 
* xmlInput = 
* "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" 
* + " <soap:Body>\r\n" + 
* "  <ns1:LoginProcesRequest xmlns:ns1=\"http://xmlns.oracle.com/GetSFDCDetailsProcess\">\r\n" 
* + "   <ns1:Login>1000</ns1:Login>\r\n" + 
* "   <ns1:Company>1000</ns1:Company>\r\n" + 
* "  </ns1:LoginProcesRequest>\r\n" + " </soap:Body>\r\n" + 
* "</soap:Envelope>"; 
*/ 

// SOAP Body 
SOAPBody soapBody = envelope.getBody(); 
SOAPElement soapBodyElem = soapBody.addChildElement(
     "LoginProcesRequest", "ns1", serverURI); 
SOAPElement soapBodyElem1 = soapBodyElem 
     .addChildElement("Login", "ns1"); 
soapBodyElem1.addTextNode(inputlogin); 
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Company", 
     "ns1"); 
soapBodyElem2.addTextNode(inputcompany); 
MimeHeaders headers = soapMessage.getMimeHeaders(); 
headers.addHeader("SOAPAction", serverURI + "LoginProcesRequest"); 
soapMessage.saveChanges(); 
/* Print the request message */ 
System.out.print("Request SOAP Message = "); 
soapMessage.writeTo(System.out); 
System.out.println(); 

您可以探索這很容易

+0

現在我可以準確地發佈我需要的東西。但我無法獲得有效回覆。我在Response中獲得了整個WSDL。 – user3441151

+0

我的代碼對於獲取響應是否正確? – user3441151

+0

是你的代碼看起來很好me.try直接通過從wsdl url中刪除?wsdl來在瀏覽器中打開你的wsdl,你會看到你必須打的xml請求。將請求轉換爲相同的格式,然後運行你的代碼 –

相關問題