本文将通过实例来讲解如何通过 HttpComponents 发起一个 GET 请求。在开始之前,我们需要引入 maven 依赖。如下:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
实例代码如下:
package com.huangx.httpcomponents; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; /** * 使用 HttpComponents 发起 GET 请求 * @author huangxin 2019/12/2 */ public class GetDemo { public static void main(String[] args) throws Exception { // (1) 创建HttpGet实例 HttpGet get = new HttpGet("https://127.0.0.1:8080/env/list"); // (2) 使用HttpClient发送get请求,获得返回结果HttpResponse HttpClient http = new DefaultHttpClient(); HttpResponse response = http.execute(get); // (3) 读取返回结果 if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } }
上面实例将使用 GET 获取 https://127.0.0.1:8080/env/list 地址的内容。如果返回的状态码(Status Code)等于200,则通过流读取返回的内容。
实例2:下面通过 URIUtils 创建一个 URI,而不是直接给出 URL 地址。实例代码如下:
package com.huangx.httpcomponents; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIUtils; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; /** * 使用 HttpComponents 发起 GET 请求 * @author huangxin 2019/12/2 */ public class GetDemo2 { public static void main(String[] args) throws Exception { // (1) 创建HttpGet实例 URI uri = URIUtils.createURI("http", "127.0.0.1", 8080, "/env/list", "", null); HttpGet get = new HttpGet(uri); // (2) 使用HttpClient发送get请求,获得返回结果HttpResponse HttpClient http = new DefaultHttpClient(); HttpResponse response = http.execute(get); // (3) 读取返回结果 if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } }
实例3:下面实例通过 URL 传递参数,例如:https://127.0.0.1:8080/env/getEnv?key=classpath, 该URL将传递一个 key 参数给服务端。代码如下:
package com.huangx.httpcomponents; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIUtils; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; /** * 使用 HttpComponents 发起 GET 请求 * @author huangxin 2019/12/2 */ public class GetDemo3 { public static void main(String[] args) throws Exception { // (1) 创建HttpGet实例 URI uri = URIUtils.createURI("http", "127.0.0.1", 8080, "/env/getEnv", "key=classpath", null); HttpGet get = new HttpGet(uri); // (2) 使用HttpClient发送get请求,获得返回结果HttpResponse HttpClient http = new DefaultHttpClient(); HttpResponse response = http.execute(get); // (3) 读取返回结果 if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } }
上面实例将向服务器发送一个 key 参数,然后将查询的结果输出到控制台。