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 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 = "";
		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) {

3 thoughts on “Sending HTTP Requests using Apache HTTP Client

  1. Madhu

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


Leave a Reply

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

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