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...

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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!!!.

Tuesday, October 21, 2008

Netbeans IDE

It’s been a long time since I have written a blog. I happen to see this contest in Netbeans and was thinking about writing about one of my most userful and efficient IDE - NetBeans.
If there is an opportunity to win 10 point y miss the bus.

I have been using Netbeans for over a year now. I believe the best feature that netbeans has is the support for swings. I have created couple of project as a hobby and have posted it in sourceforge.
http://prabeshb.sourceforge.net/ . This is a very small utility for finding similar word in different files. I was able to make this utility in very short time and with great quality.

Creating application in netbeans is one of the easiest. To create a project click on File > New Project. You can use short cut keys also to create the project Ctrl + shift + N. Once you click on the New Project, a dialogue window opens which contains many options for creating project. There are not only java application that can be created but also web application, mobility, UML, SOA, Ruby, C/C++ etc. To create a java swings application click on the Java category and select the Java Desktop Application menu under the projects section as shown in the project below.




Click on the next button and you can mention the project name and project location. After you enter the project details on click of finish button the base class and the layout is created by netbeans. So you need not worry about creating the layout and initial setting. The design view is displayed as the finish button is clicked. On the right hand side there are many palettes available that can be dragged and dropped. The drag and drop feature is very useful when creating the application. Simply drag the controls and place it where ever you like in the base panel and that will be stuck to that location. You can alter the location of the control by simply dragging the control. The figure below shows the different section for the netbeans IDE.



To run the application I prefer shortcut keys. To run the application you can use the F6 key. As you click the key the classes will start to compile and the output will be popped up. To clean and make a fresh build you can use Shift + F11.

I hope these small tips and quick guide will help you create wonderful desktop application. Do let me know if anyone needs any information or help in creating desktop application. I will be overjoyed to help you. You can write to me at:prabeshb@gmail.com

Google Custom search

Custom Search