Twitter client behind proxy

If you are writing a twitter client in java and unable to make it work behind a proxy, here is a fix:
To obtain an object of URLConnection, use this code.

private URLConnection setProxyAuthAndConnect(String uri) throws IOException {
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "ur.proxy.address");
System.setProperty("http.proxyPort", "1000");
System.setProperty("http.proxyType", "4");
System.setProperty("http.proxyUser", "username");
System.setProperty("http.proxyPassword", "pwd");
// This might be required from case to case basis

String proxyUser = “username”;
String proxyPassword = “pwd”;
URLConnection con = new URL(uri).openConnection();
con.setRequestProperty(“Proxy-Authorization”,”Basic ” + new sun.misc.BASE64Encoder().encode((proxyUser + “:” + proxyPassword).getBytes() ));
return con;

I have used jwitter library. If you are using it too you will need to modify the

I have removed:

final HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection();

and replaced with:

final HttpURLConnection connection = (HttpURLConnection)setProxyAuthAndConnect(uri);

Well hard coding is not a good idea. So you can write a function to read all the settings from a properties file. If you do not need the proxy settings then all can be set null.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s