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, 1636🔥, 0💬
Popular Posts:
iText is an ideal library for developers looking to enhance web- and other applications with dynamic...
Saxon is an open source product available under the Mozilla Public License. It provides implementati...
XML Serializer, Release 2.7.1, allows you to write out XML, HTML etc. as a stream of characters from...
What Is mail.jar of JavaMail 1.3? I got the JAR file from javamail-1_3.zip. mail.jar in javamail-1_3...
SLF4J API is a simple API that allows to plug in any desired logging library at deployment time. Her...