Java HTTP GET POST

As you know HTTP (Hypertext Transfer Protocol) is the transport protocol of www. Java has a strong support for HTTP.

HTTP addresses uses addressing syntax called URL (for example http://www.java.com). HTTP is based on TCP.

HTTP GET

This is the most widespred type of HTTP request. If you write http://www.java.com/ on your browser you have already made an HTTP GET Request. Here is a sample :

public static String doGET(String strURL) throws IOException {

String response = null;

InputStream in = null;

try {

// setup url connection

URL url = new URL(strURL);

HttpURLConnection urlConn = (HttpURLConnection) url

.openConnection();

in = urlConn.getInputStream();

StringBuilder sb = new StringBuilder();

int i;

while ((i = in.read()) > -1)

sb.append((char) i);

response = sb.toString();

} finally {

if (in != null)

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return response;

}

To run:
String getResult = doGET("http://javacream.blogspot.com/2008/04/java-http-get-post.html");
System.out.println(getResult);

HTTP POST

When you fill a form on your browser and submit it , most probably you did an HTTP Post to remote server. HTML forms uses POST method alot. In POST method , instead of adding parameters at the and of the url , you post in your request body. So , URL and Parameters are seperate. Check out this sample code for HTPP Post :

public static String doPost(String _url, String line)

throws IOException {

String response = null; //for reading from remote host

InputStream in = null; //for posting parameters to remote host

DataOutputStream out = null;

try {

// setup url connection. use POST to send forms data

URL url = new URL(_url);

HttpURLConnection urlConn = (HttpURLConnection) url

.openConnection();

urlConn.setRequestMethod("POST");

urlConn.setDoInput(true);

urlConn.setDoOutput(true);

urlConn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

out = new DataOutputStream(urlConn.getOutputStream());

out.writeBytes(line);

out.flush();

in = urlConn.getInputStream();

StringBuilder sb = new StringBuilder();

int i;

while ((i = in.read()) > -1)

sb.append((char) i);

response = sb.toString();

} finally {

if (in != null)

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

if (out != null)

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return response;

}

To run:
String result = doPost("http://www.someserver.com/cgi-bin/form", "userid=Test&Password=Test");

Download a file using HTTP GET

This is a very simple sample code for downloading a file :

public static void downloadFile(String _url, String fileName)

throws IOException {

FileOutputStream outFile = null;

outFile = new FileOutputStream(fileName);

InputStream in = null;

try {

// setup url connection.

URL url = new URL(_url);

HttpURLConnection urlConn = (HttpURLConnection) url

.openConnection();

urlConn.setUseCaches(false);

in = urlConn.getInputStream();

int i = -1;

while ((i = in.read()) > -1)

outFile.write(i);

} finally {

if (in != null)

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

if (outFile != null)

try {

outFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}


Using with proxies

You have to define proxy before you open connection to your URL.

Properties sysProps = System.getProperties();
sysProps.put("http.proxyHost","proxy.local.intranet") ;
sysProps.put("http.proxyPort", "8080") ;

URL url = new URL(_url);

HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();

If the proxy requires Authentication than the following java example code should be added:

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String basicAuthenticationUserPass =
encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
urlConn.setRequestProperty
("Proxy-Authorization", "Basic " + basicAuthenticationUserPass );

Comments

Popular posts from this blog

Connect via SSH without a password from MacOS to Linux with an SSHKEY

Read and Write Files in Java

Install Mac OS on windows laptop