Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼2625🔥, 0💬
Popular Posts:
How to perform XML Schema validation with sax\Writer.java provided in the Apache Xerces package? You...
JDK 11 jdk.management.agent.jmo dis the JMOD file for JDK 11 Management Agent module. JDK 11 Managem...
What Is poi-5.2.3.jar? poi-5.2.3.jar is one of the JAR files for Apache POI 5.2.3, which provides an...
Rhino JavaScript Java Library is an open-source implementation of JavaScript written entirely in Jav...
commons-net-1.4.1.jar is the JAR file for Apache Commons Net 1.4.1, which implements the client side...