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 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, 1502👍, 0💬
Popular Posts:
How to read XML document with DTD validation from socket connections with the socket\DelayedInput.ja.. .
Apache Commons Codec library provides implementations of common encoders and decoders such as Base64...
JDK 11 jdk.xml.dom.jmod is the JMOD file for JDK 11 XML DOM module. JDK 11 XML DOM module compiled c...
Jetty provides an HTTP server, HTTP client, and javax.servlet container. These components are open s...
What Is jniwrap.jar in JNIWrapper 3.7.1? winpack.jar in JNIWrapper 3.7.1 is an add-on JAR file for J...