Skip to main content
Chapter 16 of 18 · 1 sections

Spring: SOAP Web Service Consumption

Understand SOAP client transport configuration and the source book’s Basic authentication example.

Original chapter: SOAP WS Consumption. The text and examples below are preserved from the historical source; read the corrections above before using them.

Section 16.1: Consuming a SOAP WS with Basic auth

Create your own WSMessageSender:

Source example · PDF page 63
import java.io.IOException;
import java.net.HttpURLConnection;

import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender;

import sun.misc.BASE64Encoder;

public class CustomWSMessageSender extends HttpUrlConnectionMessageSender{

   @Override
   protected void prepareConnection(HttpURLConnection connection)
           throws IOException {

       BASE64Encoder enc = new sun.misc.BASE64Encoder();
       String userpassword = "yourUser:yourPassword";
       String encodedAuthorization = enc.encode( userpassword.getBytes() );
       connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

       super.prepareConnection(connection);
   }
}

In your WS configuration class set the MessageSender you just created:

Source example · PDF page 63
myWSClient.setMessageSender(new CustomWSMessageSender());
Buy me a coffee
Daily Practice
0/0