2017-12-24 43 views
0

この質問は重複していますが、私はStackOverflowでいくつかのソリューションを試してみましたが、依然としてSOAPリクエストの送信に問題があります。SOAP UIのようにSOAPを使用してSOAPリクエストを送信する方法

私の現在の状況は、私の顧客のWebサービスにSOAPリクエストを送信する必要があります。私はSOAP UIで、ユーザー名、パスワード、認証タイプ:Preemptiveで成功しました。今、私はJavaでこれをしたい画像に

enter image description here

を参照してください、私はすでにSOAPエンベロープを作成した、パスワードを設定せずに、応答は認証を使用していないSOAP UIと全く同じであり、それつまり、javaコードは、動作させるために認証を追加するだけです。 は、ここに私の現在のJavaコードです:

callSoapWebService(soapEndpointUrl, soapAction); 

private static void callSoapWebService(String soapEndpointUrl, String soapAction) { 
    try { 
     // Create SOAP Connection 
     SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
     SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

     // Send SOAP Message to SOAP Server 
     SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl); 

     // Print the SOAP Response 
     System.out.println("Response SOAP Message:"); 
     soapResponse.writeTo(System.out); 
     System.out.println(); 

     soapConnection.close(); 
    } catch (Exception e) { 
     System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n"); 
     e.printStackTrace(); 
    } 
} 

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception { 
    MessageFactory messageFactory = MessageFactory.newInstance(); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 

    createSoapEnvelope(soapMessage); 

    MimeHeaders headers = soapMessage.getMimeHeaders(); 
    headers.addHeader("SOAPAction", soapAction); 

    soapMessage.saveChanges(); 

    return soapMessage; 
} 

インサイドcreateSoapEnvelope()SOAPリクエストのボディを作成するためのコードがあるので、私は、私は、ヘッダーで何かをしなければならないと思います。誰でも私がSOAP UIで行ったような認証を達成するのを助けることができますか?

答えて

2

あなたは基本的な形式でユーザ名とパスワードを送信する必要があります。

String userAndPassword = String.format("%s:%s",username, password); 

String basicAuth = new sun.misc.BASE64Encoder().encode(userAndPassword.getBytes()); 
MimeHeaders mimeHeaders = message.getMimeHeaders(); 
mimeHeaders.addHeader("Authorization", "Basic " + basicAuth); 

は、Java 8の使用Base64.getEncoder().encodeToString()代わりのnew sun.misc.BASE64Encoder()を使用している場合。

関連する問題