HTTP GET请求

Maven依赖:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

导包:

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

代码:

@Slf4j
public class Test {

   /**
     * 发送get请求并返回请求结果
     *
     * @param url       请求地址
     * @param headerMap Header中携带的键值对
     * @return 返回请求结果
     */
    public static String get(String url, Map<String, String> headerMap) {
        String result = "";
        HttpGet get = new HttpGet(url);
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            if (headerMap != null && headerMap.size() > 0) {
                for (String key :
                        headerMap.keySet()) {
                    get.addHeader(key, headerMap.get(key));
                }
            }
            HttpResponse response = httpClient.execute(get);
            result = getHttpEntityContent(response);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                result = "服务器异常";
            }
        } catch (Exception e) {
            log.error("GET请求({})异常:", url, e);
        } finally {
            //终止请求
            get.abort();
        }
        //为防止频繁调用一个接口导致接口爆掉,每次调用完成后停留XXX毫秒
        try {
            Thread.sleep(Constants.HTTP_INTERVAL);
        } catch (InterruptedException e) {
            log.error("Thread.sleep()异常");
        }
        return result;
    }

    /**
     * 从返回结果中 提取内容
     *
     * @param response 请求结果
     * @return 返回
     * @throws UnsupportedOperationException 抛出
     * @throws IOException                   抛出
     */
    private static String getHttpEntityContent(HttpResponse response) throws UnsupportedOperationException, IOException {
        String result = "";
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream in = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, Constants.UTF8));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                strber.append(line);
                strber.append("\n");
            }
            br.close();
            in.close();
            result = strber.toString();
        }
        return result;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值