How to send http request using apache httpcomponents library

There are many ways to send request to a server and one of them is by using http client library. But this library has 2 versions now. The ol...

There are many ways to send request to a server and one of them is by using http client library. But this library has 2 versions now. The old httpclient and this httpcomponents. We will provide a sample code to try the latter.

But before that we need to add maven dependency to our project, this is assuming you are working on a maven project.

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>

A method that sends post request with variables:

public static String postRequest(String url, Map<String, String> params) throws IOException {
log.debug("sending POST request to={}, params={}", url, params);

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
List<NameValuePair> postParameters = new ArrayList<>();

// add post parameters
params.forEach((key, value) -> {
postParameters.add(new BasicNameValuePair(key, value));
});

// initialize the post request
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

StringBuilder responseString = new StringBuilder();

// execute the request
HttpResponse response = httpClient.execute(httpPost);

// read the response
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity responseHttpEntity = response.getEntity();
InputStream content = responseHttpEntity.getContent();

BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = buffer.readLine()) != null) {
responseString.append(line);
}

// release all resources held by the responseHttpEntity
EntityUtils.consume(responseHttpEntity);

log.debug("html-------\r\n" + responseString);

return responseString.toString();
} else {
log.error("Error sending request with status=" + response.getStatusLine().getStatusCode());
}

}

return "";
}

And finally a sample method call:

public Map<String, String> params= new HashMap<>();
params.put("param1", "value1");
params.put("param2", "value2");
params.put("param3", "value3");

MyClass.postRequest(AppSettings.MEMBER_NAME_URL, params);

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to send http request using apache httpcomponents library
How to send http request using apache httpcomponents library
toztech
https://toztech.blogspot.com/2016/12/how-to-send-http-request-using-apache.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2016/12/how-to-send-http-request-using-apache.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content