2016-08-12 8 views
6

मैं जावा में एक साबुन क्लाइंट बना रहा हूं और मुझे एक अजीब त्रुटि है।SOAPExceptionImpl खराब प्रतिक्रिया: 404 नहीं मिला अगर मैं soapMessage.writeTo (System.out) नहीं करता;

सार ग्राहक

public abstract class AbstractSoapClient { 

    private ServerContext context; 

    private String path; 

    private static final String WSSE = ""; 
    private static final String CURL = ""; 
    private static final String CURL_PASSWORD = ""; 
    private static final String SECURITY_NODE = ""; 
    private static final String USERNAME_TOKEN = ""; 
    private static final String USERNAME_NODE = ""; 
    private static final String PASSWORD_NODE = ""; 

    public AbstractSoapClient(ServerContext context) { 
     this.context = context; 
    } 

    protected SOAPMessage createRequest(String path) throws SOAPException { 
     this.path = assembleEndpoint(path); 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), this.path); 
     soapConnection.close(); 
     return soapResponse; 
    } 

    protected void setCredentials(SOAPEnvelope envelope) throws SOAPException { 
     SOAPHeader tHeader = envelope.getHeader(); 
     Name tWsseHeaderName = envelope.createName(SECURITY_NODE, WSSE, CURL); 

     SOAPHeaderElement tSecurityElement = tHeader.addHeaderElement(tWsseHeaderName); 
     tSecurityElement.setMustUnderstand(false); 

     Name tUserTokenElementName = envelope.createName(USERNAME_TOKEN, WSSE, CURL); 
     SOAPElement tUserTokenElement = tSecurityElement.addChildElement(tUserTokenElementName); 
     tUserTokenElement.removeNamespaceDeclaration(WSSE); 
     tUserTokenElement.addNamespaceDeclaration("wsu", CURL); 
     // user name child 
     Name tUsernameElementName = envelope.createName(USERNAME_NODE, WSSE, CURL); 
     SOAPElement tUsernameElement = tUserTokenElement.addChildElement(tUsernameElementName); 
     tUsernameElement.removeNamespaceDeclaration(WSSE); 
     tUsernameElement.addTextNode(context.getUsername()); 

     // password child 
     Name tPasswordElementName = envelope.createName(PASSWORD_NODE, WSSE, CURL); 
     SOAPElement tPasswordElement = tUserTokenElement.addChildElement(tPasswordElementName); 
     tPasswordElement.removeNamespaceDeclaration(WSSE); 
     tPasswordElement.setAttribute("Type", CURL_PASSWORD); 
     tPasswordElement.addTextNode(context.getPassword()); 
    } 

    private String assembleEndpoint(String path) { 
     return context.getUrl().concat(path); 
    } 

    protected abstract SOAPMessage createSOAPRequest() throws SOAPException; 

    public ServerContext getContext() { 
     return context; 
    } 

    public String getPath() { 
     return path; 
    } 

} 

साबुन ग्राहक कार्यान्वयन

public class SoapClient extends AbstractSoapClient { 

    public SoapClient(ServerContext context) { 
     super(context); 
    } 

    @Override 
    public SOAPMessage createSOAPRequest() throws SOAPException { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 
     setCredentials(envelope); 
     buildBody(envelope); 
     soapMessage.saveChanges(); 
     try { 
      soapMessage.writeTo(System.out); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return soapMessage; 
    } 

    private void buildBody(SOAPEnvelope envelope) throws SOAPException { 
     envelope.addNamespaceDeclaration("sch", "------"); 
     SOAPBody soapBody = envelope.getBody(); 
     SOAPElement soapBodyElem = soapBody.addChildElement("sampleData", "sampleData"); 
     SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem1.addTextNode("sampleData"); 
     SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem2.addTextNode("sampleData"); 
     SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem3.addTextNode("Y"); 
     SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem4.addTextNode("sampleData"); 
     SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem5.addTextNode("sampleData"); 
     SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("sampleData"); 
     soapBodyElem6.addTextNode("sampleData"); 
    } 

    public static void main(String[] args) throws SOAPException, IOException { 
     SoapClient client = new SoapClient(
       new ServerContext("url", "user", "password")); 
     SOAPMessage response = client.createRequest("endpoint"); 
     response.writeTo(System.out); 
    } 

} 

अजीब बात कोड के इस हिस्से में है:

try { 
       soapMessage.writeTo(System.out); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

अगर मैं कॉम इस कोड है कि केवल अनुरोध प्रिंट ent से पहले इसे भेजने के लिए मैं अगले अपवाद:

ago 12, 2016 12:58:17 PM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post 
    GRAVE: SAAJ0008: respuesta errónea; Not Found 
    Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404Not Found 
     at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149) 
     at 
AbstractSoapClient.createRequest(AbstractSoapClient.java:44) 
     at SoapClient.main(SoapClient.java:67) 
    Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404Not Found 
     at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264) 
     at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145) 
     ... 2 more 

    CAUSE: 

    com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404Not Found 
     at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264) 
     at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145) 
     at AbstractSoapClient.createRequest(AbstractSoapClient.java:44) 
     at SoapClient.main(SoapClient.java:67) 

लेकिन मैं इस लाइन टिप्पणी नहीं है, तो मैं प्रतिक्रिया सही ढंग से प्राप्त कर सकते हैं, मेरे लिए इन मतलब नहीं है क्योंकि यह 404 नहीं मिला है यदि मैं इसे भेजने से पहले कंसोल में अनुरोध नहीं लिखता हूं।

उत्तर

1

यदि आप writeTo implementation चेक करते हैं तो आप देखेंगे कि वे एक SOAPAction शीर्षलेख जोड़ते हैं।

MessageFactory messageFactory = MessageFactory.newInstance(); 
SOAPMessage soapMessage = messageFactory.createMessage(); 
soapMessage.getMimeHeaders().addHeader("SOAPAction", "\"\""); 

आशा है कि यह मदद करता है:

का अनुसरण करें।

+0

लेकिन पर एक नज़र क्यों एक writeTo विधि है कि केवल एक धारा में अनुरोध संदेश के लिए हेडर जोड़ने प्रिंट, वास्तव में एक अच्छा अभ्यास है कि है? –

+0

मुझे पता है कि यह अजीब लगता है, और निश्चित रूप से एक अच्छा अभ्यास नहीं है, हालांकि MessageImpl.write स्रोत कोड ऐसा करता है। – fernandospr

0

डिफ़ॉल्ट रूप से, SOAPMessage इंटरफ़ेस SoapMessageImpl द्वारा कार्यान्वित किया गया है। यदि कार्यान्वयन मौजूद नहीं है तो इस कार्यान्वयन में SOAPAction शीर्षलेख जोड़ने का दुष्प्रभाव होता है।

writeTo को कॉल करने के बाद, आप इसे फोन करके निकाल सकते हैं:

soapMessage.getMimeHeaders().removeHeader("SOAPAction"); 

कहा करने के बाद अतिरिक्त कोड जोड़ने के बजाय केवल कॉल और प्रतिक्रिया लॉग इन करने की है कि,, मैं बजाय एक प्रॉक्सी का उपयोग करने के लिए सुझाव देते हैं।

आप ग्रहण का उपयोग कर रहे हैं, तो TCP/Monitor View

संबंधित मुद्दे