Run ClientWithResponseHandler.java HttpComponents Client Example

Q

How to run the ClientWithResponseHandler.java HttpComponents Client Example? I have httpcomponents-client-4.5.3-bin.zip installed.

✍: FYIcenter.com

A

If you have httpcomponents-client-4.5.3-bin.zip installed, you can follow this tutorial to run the ClientWithResponseHandler.java HttpComponents Client Example:

1. Open the example program file ClientWithResponseHandler.java from \fyicenter\httpcomponents-client-4.5.3\examples\org\apache\http\examples\client folder:

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * ...
 */
package org.apache.http.examples.client;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * This example demonstrates the use of the {@link ResponseHandler} to simplify
 * the process of processing the HTTP response and releasing associated resources.
 */
public class ClientWithResponseHandler {

    public final static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://httpbin.org/");

            System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                @Override
                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }
            };
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
        } finally {
            httpclient.close();
        }
    }

}

2. Compile and run the example with Java SE 8 JDK:

\fyicenter\httpcomponents-client-4.5.3\examples>\fyicenter\jdk-1.8.0\bin\javac
   -cp ..\lib\httpclient-4.5.3.jar;..\lib\httpcore-4.4.6.jar 
   org\apache\http\examples\client\ClientWithResponseHandler.java

\fyicenter\httpcomponents-client-4.5.3\examples>\fyicenter\jdk-1.8.0\bin\java
   -cp .\;..\lib\httpclient-4.5.3.jar;..\lib\httpcore-4.4.6.jar;..\lib\commons-logging-1.2.jar 
   org.apache.http.examples.client.ClientWithResponseHandler
   > output.txt 
   
\fyicenter\httpcomponents-client-4.5.3\examples>type output.txt | more

Executing request GET http://httpbin.org/ HTTP/1.1
----------------------------------------
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv='content-type' value='text/html;charset=utf8'>
  <meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tre
e/0.7.3)'>
  <title>httpbin(1): HTTP Client Testing Service</title>
  <style type='text/css' media='all'>
  /* style: man */
  body#manpage {margin:0}
...

 

Run ClientChunkEncodedPost.java HttpComponents Client Example

Run QuickStart.java HttpComponents Client Example

Using HttpComponents API in Java Programs

⇑⇑ FAQ for Apache HttpComponents JAR Library

2017-11-02, 1628🔥, 0💬