HttpComponents发送Get请求

本文将介绍怎样使用 HttpComponents 发起一个 Get请求,使用 Get请求去获取数据,以及携带参数给服务端。

本文将通过实例来讲解如何通过 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 参数,然后将查询的结果输出到控制台。

人生就像赛跑,不在乎你是否第一个到达终点,而在乎你有没有跑完全程。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号