Skip to content

HttpConnection with Proxy

April 1, 2009

After lots of hunting got it right finally:


  URL url = new URL("http://mysite.wordpress.com/xmlrpc.php");
        InetSocketAddress inet = new InetSocketAddress("proxyHost.com", 8080);
        HttpURLConnection connection = ( HttpURLConnection ) url.openConnection( new Proxy(Proxy.Type.HTTP , inet));
        String proxyUser = "user" ;
        String proxyPassword = "pass" ;
        Properties p  =System.getProperties();
        connection.setRequestProperty(
                   "Proxy-Authorization",
                   (new StringBuilder("Basic ")).
                            append((new BASE64Encoder()).encode((new StringBuilder(String.valueOf(proxyUser)))
                        .append(":")
                        .append(proxyPassword).toString().getBytes())).toString());

            connection.setDoInput( true );
            connection.setDoOutput( true );
            connection.setRequestMethod("POST");
            //connection.setRequestProperty("Content-Length","0");
            connection.setRequestProperty(
                "Content-Type", "text/xml; charset=" +
             XmlRpcMessages.getString( "XmlRpcClient.Encoding"));

             connection.connect();
             DataInputStream dis  = new DataInputStream(connection.getInputStream());
             byte []b = new byte[1];
             while (  -1 != dis.read(b,0,1))
             {
                 System.out.print(new String(b));
             }

Set the following in java opts to specify proxy settings:

-Dhttp.proxyHost=

-Dhttp.proxyPort=

-Dhttp.proxyPassword=

-Dhttp.proxyUser=

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: