Java: creare una connession HttpURLConnection passando da un proxy

Se si vuole impostare un proxy ad una connessione realizzata con la class HttpURLConnection è sufficiente fare così:

URL mioURL = new URL (s );
 
// L'indirizzo IP del proxy (es 192.168.1.1)
byte b[] = new byte[4];
b[0] = new Integer(192).byteValue();
b[1] = new Integer(168).byteValue();
b[2] = new Integer(1).byteValue();
b[3] = new Integer(1).byteValue();
 
// 80 è il numero di porta 
InetSocketAddress inet = new InetSocketAddress(InetAddress.getByAddress(b), 80);
Proxy proxy = new Proxy(Proxy.Type.HTTP,inet); 		
 
HttpURLConnection huc = ( HttpURLConnection ) mioURL.openConnection ( proxy ) ;

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>