If you are using https protocol to invoke axis2 web service, you can implements your own SecureProtocolSocketFactory like this:
public class CustomProtocolSocketFactory implements SecureProtocolSocketFactory {
private static final Logger logger = Logger.getLogger("trace");
InetAddress localAddress=null;
SSLContext ctx=null;
Socket socket=null;
public CustomProtocolSocketFactory(SSLContext ctx) {
this.ctx = ctx;
}
public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress) {
this.ctx = ctx;
this.localAddress=localAddress;
}
public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress, Socket socket) {
this.ctx = ctx;
this.localAddress=localAddress;
this.socket=socket;
}
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
throws IOException, UnknownHostException {
logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort)");
return ctx.getSocketFactory().createSocket(host, port, localAddress, localPort);
}
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort,HttpConnectionParams params)");
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = ctx.getSocketFactory();
if (timeout == 0) {
if(this.localAddress!=null){
return socketfactory.createSocket(host, port, this.localAddress, localPort);
}
else{
return socketfactory.createSocket(host, port, localAddress, localPort);
}
} else {
Socket socket = socketfactory.createSocket();
if(this.localAddress!=null){
SocketAddress localaddr = new InetSocketAddress(this.localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
else{
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
if(localAddress!=null){
logger.debug("createSocket(String host, int port) localAddress not null");
return createSocket(host, port, localAddress, 0);
}
else{
logger.debug("createSocket(String host, int port)");
return ctx.getSocketFactory().createSocket(host, port);
}
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
logger.debug("createSocket(Socket socket, String host, int port, boolean autoClose)");
return ctx.getSocketFactory().createSocket(socket, host, port, autoClose);
}
}
in a way that you can bind the socket with InetAddress localAddress and use like this:
proxy._getServiceClient().getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
new Protocol("https",(ProtocolSocketFactory)new CustomProtocolSocketFactory(context,localAddress),port));