Conéctese a un sitio usando un código proxy en java
Quiero conectarme como sitio a través de proxy en java. Este es el código que he escrito:
public class ConnectThroughProxy
{
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080));
public static void main(String[] args)
{
try
{
URL url = new URL("http://www.rgagnon.com/javadetails/java-0085.html");
URLConnection connection=url.openConnection();
String encoded = new String(Base64.encode(new String("user_name:pass_word").getBytes()));
connection.setDoOutput(true);
connection.setRequestProperty("Proxy-Authorization","Basic "+encoded);
String page="";
String line;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line=in.readLine()) != null)
{
page.concat(line + "\n");
}
System.out.println(page);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Al intentar ejecutar este código, arroja el siguiente error:
java.lang.IllegalArgumentException: caracteres ilegales en el valor del encabezado del mensaje: Basic dXNlcl9uYW1lOnBhc3Nfd29yZA ==
en sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader (HttpURLConnection.java:323)
en sun.net.www.protocol.http.HttpURLConnection.setRequestProperty (HttpURLConnection.java:2054)
en test.ConnectThroughProxy.main (ConnectThroughProxy.java:30)
¿Alguna idea de cómo hacerlo?