Run ClientChunkEncodedPost.java HttpComponents Client Example

Q

How to run the ClientChunkEncodedPost.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 ClientChunkEncodedPost.java HttpComponents Client Example:

1. Open the example program file ClientChunkEncodedPost.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.File;
import java.io.FileInputStream;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Example how to use unbuffered chunk-encoded POST request.
 */
public class ClientChunkEncodedPost {

    public static void main(String[] args) throws Exception {
        if (args.length != 1)  {
            System.out.println("File path not given");
            System.exit(1);
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost("http://httpbin.org/post");

            File file = new File(args[0]);

            InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM);
            reqEntity.setChunked(true);
            // It may be more appropriate to use FileEntity class in this particular
            // instance but we are using a more generic InputStreamEntity to demonstrate
            // the capability to stream out data from any arbitrary source
            //
            // FileEntity entity = new FileEntity(file, "binary/octet-stream");

            httppost.setEntity(reqEntity);

            System.out.println("Executing request: " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } 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.ClientChunkEncodedPost
   \windows\system32\ping.exe
   
Executing request: POST http://httpbin.org/post HTTP/1.1
----------------------------------------
HTTP/1.1 503 Service Unavailable
<!DOCTYPE html>
  <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <title>Application Error</title> 
...

Unfortunately, the service at http://httpbin.org/post is not available to confirm the test.

 

Run PrintVersionInfo.java HttpComponents Core Example

Run ClientWithResponseHandler.java HttpComponents Client Example

Using HttpComponents API in Java Programs

⇑⇑ FAQ for Apache HttpComponents JAR Library

2017-11-02, 1694🔥, 0💬