Apache DefaultHttpClient Tutorial: execute HTTP GET request with params and BasicAuthentication

11 05 2013

When dealing with REST webservices, the HTTP GET request is commonly used (very often with params). Apache’s DefaultHttpClient has convenient methods that utilize making such requests. This is how I do it:

Request data

the URL that I am going to execute is:


GET request params are as follows:

  • body: message body (content)
  • from: sender’s name
  • to: recipient’s name

so the URL with params attached will look like this:



If your JRE is not equipped with Apache DefaultHttpClient, then you need to download two jars (I recommend to use version no older than 4.2.1):

  • httpclient-4.2.1.jar
  • httpcore-4.2.1.jar

1. Create URL

You can do it in two ways: hardcoded or objective.

Hardcoded version is simply creating String variable like this:

String url = "http://www.example.org:8001/rest/sendMessage?body=hello&from=jack&to=bill";

It is not convenient, because You have to converts this String to URI object, and encode it. The url above does not need encoding, but it would, if for example message content would contain spaces (The space is encoded to %20). So if body would be:

hello world

then the param would be encoded to:


More about ecoding you will find on wiki page here.

Objective version requires httpclient in version 4.2.1 or higher. That release has URIBuilder:

URIBuilder builder = new URIBuilder();
.setParameter("msg", "hello")
.setParameter("from", "jack")
.setParameter("to", "bill");
URI url = builder.build();

the build method is encoding the url automatically, so the result uri is ready to execute. In other words you can write:

.setParameter("msg", "hello world")

without taking care about encoding – the builder will do it for you. If you do not need params, then you simply omit the setParameter() methods.

2. Create HttpGet instance

HttpGet request object  is created with url:

HttpGet httpget = new HttpGet(url);

3. Create Http Client

The DefaultHttpClient is powerful enough in most cases. The client is responsible for executing requests:

DefaultHttpClient httpClient = new DefaultHttpClient();

4. Provide BasicAuthentication credentials (Optional)

If your REST API is protected with BasicAuthentication you can add an Authentication Header to your HttpClient:

Credentials credentials = new UsernamePasswordCredentials("username", "password");

5. Execute GET request

Now it’s time to execute request on client and retrieve the response:

HttpResponse response = httpClient.execute(httpget);

6. Handle all exceptions

There are several exceptions thrown by methods above. Your IDE will enforce you to handle them. Please see my implementation in snippet below.

7. See my complete source code :)

If you have problems building it up all together, here is my full implementation:

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;

public void sendTestMessage() {
        // build URL
	URIBuilder builder =newURIBuilder();
	URI url = builder.build();

	HttpGet httpget =newHttpGet(url);// Execute HTTP Get Request
	DefaultHttpClient httpClient =new DefaultHttpClient();
	Credentials credentials =new UsernamePasswordCredentials("username", "password");

	HttpResponse response = httpClient.execute(httpget);
    }catch(ClientProtocolException e) {
    }catch(IOException e) {
    }catch(URISyntaxException e) {

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




4 responses

25 06 2013

Algunos post me agradaron algo mas… ;)

20 07 2013

It is not my first time to go to see this web site, i
am visiting this web page dailly and obtain good facts from here every day.

21 03 2014
Kartikeya Upasani

That was simply put and easy to grasp.
Thanks a lot :)

27 07 2014
Best Android Apps For The PC

Thanks for ones marvelous posting! I genuinely enjoyed reasding it, you could be a great author.I will be sure to bookmark your blog
and will eventully come back very soon. I want to
enncourage you to definitely continue your great work, have a nice afternoon!

Give Your feedback:

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: