Tuesday 21 May 2024

Http Client 5: Get the response code

In this brief tutorial, I'll demonstrate how to retrieve and authenticate the HTTP Response's StatusCode using HttpClient.

After sending the Http request – we get back an instance of org.apache.hc.client5.http.impl.classic.ClassicHttpResponse – which allows us to access directly the Status Code:

response.getCode()

Example

ClassicHttpRequest request = new HttpGet("https://self-learning-java-tutorial.blogspot.com");
ClassicHttpResponse response = httpClient.execute(request, responseHandler);
int responseCode = response.getCode();

Find the below working application.

 

ResponseCodeDemo.java

package com.sample.app.httpclient;

import java.io.IOException;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;

public class ResponseCodeDemo  {

	public static void main(String[] args) {

		HttpClientResponseHandler<ClassicHttpResponse> responseHandler = new HttpClientResponseHandler<ClassicHttpResponse>() {
			@Override
			public ClassicHttpResponse handleResponse(final ClassicHttpResponse response) throws IOException, ParseException {
				return response;
				
			}
		};

		try (CloseableHttpClient httpClient = HttpClients.custom().disableAuthCaching().build();) {
			
			ClassicHttpRequest request = new HttpGet("https://self-learning-java-tutorial.blogspot.com");
			ClassicHttpResponse response = httpClient.execute(request, responseHandler);
			int responseCode = response.getCode();
			
			System.out.println("responseCode : " + responseCode);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output

responseCode : 200

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment