Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
Run ClientChunkEncodedPost.java HttpComponents Client Example
How to run the ClientChunkEncodedPost.java HttpComponents Client Example? I have httpcomponents-client-4.5.3-bin.zip installed.
✍: FYIcenter.com
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
2017-11-02, 1784🔥, 0💬
Popular Posts:
SLF4J API is a simple API that allows to plug in any desired logging library at deployment time. Her...
Apache Axis2 is the core engine for Web services. It is a complete re-design and re-write of the wid...
Rhino JavaScript Java Library is an open-source implementation of JavaScript written entirely in Jav...
How to download and install ojdbc5.jar for Oracle 11g R1? ojdbc5.jar for Oracle 11g R1 is a Java 5 J...
This package is the backport of java.util.concurrent API, introduced in Java 5.0 and further refined...