Connect to a site using proxy code in java

Posted by Nithin on Stack Overflow See other posts from Stack Overflow or by Nithin
Published on 2010-06-14T08:55:53Z Indexed on 2010/06/15 21:22 UTC
Read the original article Hit count: 476

Filed under:

I want to connect to as site through proxy in java.This is the code which I have written

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();
    }
}

while trying to run this code it throws the following error java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic dXNlcl9uYW1lOnBhc3Nfd29yZA==

    at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)
    at test.ConnectThroughProxy.main(ConnectThroughProxy.java:30)

Any Idea how to do it.

© Stack Overflow or respective owner

Related posts about java