How to send http request using apache httpcomponents library
Tech-Today

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 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);




- How To Download A File In Angular2
There are 2 ways I use to download a file with Angular2 or greater. For this example, we will use a Java REST service. The first approach would be taking advantage of the HTTP download, where the Angular side will just call a URL that will initiate the...

- Display A Byte[] Image From A Rest Web Service
This tutorial will not delve into angular nor will we discuss what a directive is. We will not try to explain what a rest web service is. Instead we will provide a demonstration via actual code, how we can consume a rest service from angular that returns...

- How To Create A Data Table In Angular Using Ngtable
I'm trying to evaluate front end frameworks since it's becoming more and more popular lately (I'm more of a backend developer using primefaces). And so I'm looking at angular2 and react, but in this tutorial we will be trying to develop...

- Jax-rs 2.0 Security Tutorial In Javaee And Jboss
This tutorial will summarize how the author was able to call a secured rest webservice using resteasy. We will not go into detail on how we build the entire project since the code is already pushed at github. Basically we will just note down the most...

- How To Generate A Jasper Report In Java
This tutorial will guide you in generating a jasper report in java. You need: 1.) Eclipse 2.) iReport - http://community.jaspersoft.com/project/ireport-designer Steps: 1.) First we need to create a model for our report. For example we have a Student...



Tech-Today








.