HttpClient_get请求

2023-01-13 10:58:58
package com.test.httpClient;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@SpringBootTest
@RunWith(SpringRunner.class)
public class HttpClient {
    /**
     * **使用httpClient发送get请求  添加请求头,解决跨域,防盗链问题**
     */
    @Test
    public void httpClient_Get() throws IOException {
        //可关闭的httpClient,相当于打开的一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //定义一个需要请求的对象
        String strUrl = "https://www.baidu.com/";
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(strUrl);

        **/*httpClient_get也可以添加请求头*/**
        **// User-Agent,解决httpClient认为不是真人行为  (跨域好像可以这么用,网站如果访问限制爬取的话也可以这么用)**
        httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36");
        **//防盗链, value是发生防盗链的网站的url,只要是访问网站的链接就行,就会认为是自己网站的请求**
        httpGet.addHeader("Referer","https://www.baidu.com/");

        //可关闭的响应,用于获取响应回来的数据
        CloseableHttpResponse response = null;
        try {
            //httpclient客户端执行需要请求的对象,捕获异常
            response = httpClient.execute(httpGet);
            /*
            获取响应的结果 : DecompressingEntity
            HttpClient不仅可以作为结果,也可以作为请求的参数实体,有很多的实现
            */
            //响应的结果
            HttpEntity entity = response.getEntity();
            //EntityUtils是对Entity进行操作的工具类
            String resultStr = EntityUtils.toString(entity, StandardCharsets.UTF_8);

            System.out.println(resultStr);
            //确保释放资源,释放流
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            httpClient.close();
            response.close();
        }
    }

    **/**
     * 使用httpClient发送get请求  带参数,如果参数中有特殊字符接收方可能出现乱码,可以请求前处理解决乱码
     */**
    @Test
    public void httpClient_Get_Param() throws IOException {
        //可关闭的httpClient,相当于打开的一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //定义一个需要请求的对象  设置为自己的接口,为测试特殊字符参数的乱码
        String strUrl = "http://localhost:8888/httpclient/test?password=";

        //httpclient的get请求添加参数  有特殊字符
        String passwordParam = "123+abc|";
        //给密码设置编码
        String encodePassword = URLEncoder.encode(passwordParam, StandardCharsets.UTF_8.name());

        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(strUrl+encodePassword);


        /*httpClient_get也可以添加请求头*/
        // User-Agent,解决httpClient认为不是真人行为  (跨域好像可以这么用,网站如果访问限制爬取的话也可以这么用)
        httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36");
        //防盗链, value是发生防盗链的网站的url,只要是访问网站的链接就行,就会认为是自己网站的请求
        httpGet.addHeader("Referer","https://www.baidu.com/");
        //可关闭的响应,用于获取响应回来的数据
        CloseableHttpResponse response = null;
        try {
            //httpclient客户端执行需要请求的对象,捕获异常
            response = httpClient.execute(httpGet);
            /*
            获取响应的结果 : DecompressingEntity
            HttpClient不仅可以作为结果,也可以作为请求的参数实体,有很多的实现
            */
            //响应的结果
            HttpEntity entity = response.getEntity();
            //EntityUtils是对Entity进行操作的工具类
            String resultStr = EntityUtils.toString(entity, StandardCharsets.UTF_8);

            System.out.println(resultStr);
            //确保释放资源,释放流
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            httpClient.close();
            response.close();
        }
    }
}

请求的接口

package com.test.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/httpclient")
public class HttpClientController {
    @GetMapping("/test")
    public String httpClientTest(@RequestParam("password") String password) {
        System.out.println("请求的参数为 : " + password);
        return "httpClient 接收成功";
    }
}

  • 作者:爱穿衬衫的张某某
  • 原文链接:https://blog.csdn.net/weixin_46649054/article/details/117004406
    更新时间:2023-01-13 10:58:58