2016-08-12 5 views
6

私はJavaでSoapクライアントを作成していますが、私は奇妙なエラーが発生しています。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; 
    } 

} 

SOAPクライアントの実装

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(); 
      } 

私はCOMM場合

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) 

しかし、私は、この行をコメントしていない場合、私は、正しく応答を得ることができます私のために、これらはので意味がありません:要求だけを印刷し、このコードをENT前に、私は次の例外を取得、それを送信しますなぜ私は404を見つけられませんでした。送信する前にコンソールに要求を書き込まないと、が見つかりませんでした。

答えて

1

writeTo implementationをチェックすると、SOAPActionヘッダーが追加されます。

次のことを試してみてください。

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

はそれがお役に立てば幸いです。

+0

を見てみましょう。しかし、なぜストリームのみで要求を印刷するのwriteToメソッドは、メッセージにヘッダを追加し、それは本当に良い練習ですか? –

+0

私はそれが奇妙に聞こえますが、確かに良い習慣ではありませんが、MessageImpl.writeToソースコードはそれを行います。 – fernandospr

0

デフォルトでは、SOAPMessageのインターフェイスはSoapMessageImplで実装されています。 この実装には、存在しない場合はSOAPActionヘッダーを追加するという副作用があります。

のwriteToに呼び出した後、あなたが呼び出すことによって、それを削除することができます

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

が代わりに余分なコードを追加するだけのコールとレスポンスをログに記録する、ことを言って、私の代わりにプロキシを使用することをお勧めします。

Eclipseを使用している場合、TCP/Monitor View

関連する問題