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 ClientWithResponseHandler.java HttpComponents Client Example
How to run the ClientWithResponseHandler.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 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
2017-11-02, 1764🔥, 0💬
Popular Posts:
What Is XMLBeans xbean.jar 2.6.0? XMLBeans xbean.jar 2.6.0 is the JAR file for Apache XMLBeans 2.6.0...
What Is log4j-1.2.15.jar? I got the JAR file from apache-log4j-1.2.15.zip. log4j-1.2.15.jar is the v...
Rhino JavaScript Java Library is an open-source implementation of JavaScript written entirely in Jav...
How to download and install JDK (Java Development Kit) 5? If you want to write Java applications, yo...
XStream is a simple library to serialize objects to XML and back again. JAR File Size and Download L...