2 Minutes To Code (β Version)

Apache Commons Http Client - 2 minute tutorial

Efficient usage : How to make an http request efficiently

This example shows how to make an http get request and read the http response efficiently.

Unlike in the previous page's example, here the response is read using the method getResponseBodyAsStream() with the http response's character set obtained from the method getResponseCharSet() of GetMethod object.
package com.techfundaes.httpBag;

import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class SimpleGetEfficient
	/* The only difference with SimpleGet.java is use of "getMethod.getResponseBodyAsStream()" instead of 
		"getMethod.getResponseBodyAsString()". The method "getResponseBodyAsString()" will put the complete http
		response in a string object in memory. This can cause memory issues like jvm crash/jvm slowing down.
	public static void main(String[] args)
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod("http://www.techfundaes.com/test.do");
			Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet());
			int characterRead = reader.read(); //Then persist to a file, db or process as required
		catch (Exception e)