Sending HTTP Requests using Apache HTTP Client

Sending HTTP Requests using Apache HTTP Client:

Below is the sample code to test end points using Apache Http Client in Java
This is a GET Method.

package org.apache.http.examples.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class GetLibrary {
	
	public static void main(String args[]) throws Exception {
		String url = "http://www.google.com/search?q=httpClient";
		 
		HttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);		
		request.addHeader("User-Agent", "USER-AGENT");
		HttpResponse response = client.execute(request);	 
		System.out.println("Response Code : " 
	                + response.getStatusLine().getStatusCode());	 
		BufferedReader rd = new BufferedReader(
			new InputStreamReader(response.getEntity().getContent()));	 
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}
		System.out.println(result);
	}
}
Advertisements

3 thoughts on “Sending HTTP Requests using Apache HTTP Client

  1. HttpPost(url); –> Post Method, HttpDeleteWithBody(url); –> Delete Method, HttpPut(url); –> Put Method.

    Like

Leave a Reply

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s