You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Jose Escobar <eb...@gmail.com> on 2012/09/24 16:04:46 UTC
Set outgoing connection port on httpclient
Hello to all,
A paranoid firewall rule force me to use a range of outgoing ports on
a httpclient based aplication. Is there an easy way to configure this
on httpclient?
Meanwhile, I don't find that easy way so I have developed a custom
socket (also for ssl) I want to share:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Random;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomPlainSocketFactory extends PlainSocketFactory {
private int portMin=-1;
private int portMax=-1;
private static final int MAX_RETRIES=20;
private Random randomGenerator;
private final Logger logger =
LoggerFactory.getLogger(CustomPlainSocketFactory.class);
public CustomPlainSocketFactory(int portMin, int portMax, Random
randomGenerator) {
super();
this.portMin=portMin;
this.portMax=portMax;
this.randomGenerator=randomGenerator;
}
@Override
public Socket connectSocket(
final Socket socket,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpParams params) throws IOException,
ConnectTimeoutException {
if (remoteAddress == null) {
throw new IllegalArgumentException("Remote address may not
be null");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may
not be null");
}
Socket sock = socket;
if (sock == null) {
sock = createSocket();
}
if(portMin>0 && portMax>0 && portMax>portMin){
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
boolean binded=false;
int bindRetries=0;
InetSocketAddress localAddressPort;
while(!binded && bindRetries<MAX_RETRIES){
bindRetries++;
localAddressPort=new
InetSocketAddress(portMin+randomGenerator.nextInt(portMax-portMin));
logger.info("Tratando de conseguir un socket de salida con
puerto {}", localAddressPort.getPort());
try{
sock.bind(localAddressPort);
binded=true;
}catch(Exception ex){
binded=false;
}
}
}
else{
if (localAddress != null) {
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
sock.bind(localAddress);
}
}
int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
int soTimeout = HttpConnectionParams.getSoTimeout(params);
try {
sock.setSoTimeout(soTimeout);
sock.connect(remoteAddress, connTimeout);
} catch (SocketTimeoutException ex) {
throw new ConnectTimeoutException("Connect to " +
remoteAddress + " timed out");
}
return sock;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: Set outgoing connection port on httpclient
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2012-09-24 at 16:04 +0200, Jose Escobar wrote:
> Hello to all,
>
> A paranoid firewall rule force me to use a range of outgoing ports on
> a httpclient based aplication. Is there an easy way to configure this
> on httpclient?
Try using the ConnRoutePNames#LOCAL_ADDRESS ('http.route.local-address')
parameter
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e490
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org