HTTP Post Example 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 POST Method.

package org.apache.http.examples.client;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

public class PostLibrary {
	public static void main(String args[]) throws Exception {
		String url = "http://localhost/PublicAuthentication/";
		HttpClient client = HttpClientBuilder.create().build();
		HttpPost post = new HttpPost(url);	 
		// add header
		post.setHeader("Content-Type", "Application/JSON");
		//Compose JSON
		post.setEntity(new StringEntity("{\"username\":\"\", \"password\":\"password\"}"));
		//post.setEntity(new UrlEncodedFormEntity(urlParameters));	 
		HttpResponse response = client.execute(post);
		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) {


One thought on “HTTP Post Example using Apache Http Client

  1. It’s hard to find your website in google. I found it on 17 spot, you should build quality backlinks , it will help you to get more visitors.
    I know how to help you, just type in google – k2 seo tips


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 )

Google+ photo

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

Connecting to %s