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 QuickStart.java HttpComponents Client Example
How to run the QuickStart.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 QuickStart.java HttpComponents Client Example:
1. Open the example program file QuickStart.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.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class QuickStart {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet("http://httpbin.org/get");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
}
2. Compile the example with Java SE 8 JDK compiler:
\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\QuickStart.java
3. Run the example:
\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.QuickStart HTTP/1.1 200 OK HTTP/1.1 200 OK
⇒ Run ClientWithResponseHandler.java HttpComponents Client Example
⇐ Using HttpComponents API in Java Programs
2017-11-05, ∼2335🔥, 0💬
Popular Posts:
JDK 11 jdk.internal.le.jmod is the JMOD file for JDK 11 Internal Line Editing module. JDK 11 Interna...
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
What Is mail.jar of JavaMail 1.3? I got the JAR file from javamail-1_3.zip. mail.jar in javamail-1_3...
commons-net.jar is the bytecode of Apache Commons Net library, which implements the client side of m...
Apache ZooKeeper is an open-source server which enables highly reliable distributed coordination. Ap...