Featured Post

Field Marshal Sam Manekshaw on leadership

Field Marshal Sam Manekshaw on leadership I had a chance to read this article recently and just loved the way he describes leadership...

Monday, May 25, 2009

HttpConnection using java

Many of you might be wondering how to make a HttpConnection from java. Here is a small program that helps you to make the connection from pure java code. This has been tested and I have been using this from a long time. It’s very simple and easy to use. If required you can just copy and start using this code, I don’t think you will need to change any part of this code. It’s ready to use as is. Just place this code in your utli folder of the code and just call this method. That’s it.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

/**
* This class is used to make the HTTP Connection from a java code.
* @author Prabesh Bhaskaran
*/
public class MakeHttpConnection {

/**
* The getConnection method takes 2 parameters, the URL string of the site that you would like to connect
* and the other parameter is the parameter that you would like to pass through the query.
* @param urlString: The URL of the site that you would like to connect
* @param query: The parameter that you would like to pass to the site.
* @throws java.lang.Exception
* @return
*/
public String getConnection(String urlString, String query) throws Exception {
String retStr = "";
URL url = new URL(urlString);
String responseString = "";

HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setAllowUserInteraction(false);
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded"); // the content-length should not be necessary, but we're cautious

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
bw.write(query);
bw.write("\r\n");
bw.flush();
bw.close();

if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;

while ((inputLine = br.readLine()) != null) {
responseString = responseString+inputLine;
}
System.out.println("responseString="+responseString);
}
return responseString;
}
}

As the saying goes
Necessity is the mother of all invention
this program was also developed when there was a necessity. My office had blocked many sites from viewing due to some junk reasons, but I needed some sites that would help me. So from a java code I just make a httpconnection and call the required site, this code will return a string that will give the full website page. That’s it I was able to view the website even when my office had blocked that particular site.

Go ahead and try out. Let me know if you need any help or if you come across any issues when using this code.

Happy coding!!!.

4 comments:

Anonymous said...

Nope
Its failing for all filtered sites viz
http://www.bbcworld.com
Http://www.gmail.com
etc

Prabeshb said...

Hi,
For those sites you will need to change the urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");

The URLencoder is of different format for those site. You will need to add the respective.
I beleive for gmail the content type is "text/html; charset=UTF-8". You can try with this. I did not get time to test this. If i get time i will test it and update the code.

Thanks for trying out the code and posting the issue.

Regards,
Prabesh Bhaskaran

Anonymous said...

I tried for gmail with

urlConnection.setRequestProperty("Content-type","text/html; charset=UTF-8")

But again the same result. The site is filtered

The only exception is with http://in.rediff.com
wherein I got some responseString and its able to open up the home page.

Prabeshb said...

Hi,
I have not tried with gmail.
May be over the weekend i will try with gmail and let you know.
Is there any other contentType that you have tried?
Let me know if you have tried any other contentType.
Regards,
Prabesh

Google Custom search

Custom Search