展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Http Client入门

参考

简介

  • 导入依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 自动打包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- maven编译 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • yml配置文件
server:
  port: 8080
spring:
  application:
    name: demo01
logging:
  level:
    com:
      ychen:
        demo01: debug
  • 启动类
@SpringBootApplication
public class Demo01Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
    }
}
  • 测试1: 发送无参get请求
    @GetMapping("/test1")
    @ResponseBody
    public String test1(){
        // 创建httpclient客户端对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 构建get请求对象
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        // 构建响应对象
        CloseableHttpResponse response = null;
        try {
            // 发送请求
            response = httpClient.execute(httpGet);
            // 返回结果
            HttpEntity entity = response.getEntity();
            // 获取响应结果
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
            // 关闭流
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null){
                    response.close();
                }
                if (httpGet != null){
                    httpGet.releaseConnection();
                }
                if (httpClient!= null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // end
        return "success";
    }
  • 测试2: 发送午餐get请求,并添加请求头
    @GetMapping("/test2")
    @ResponseBody
    public String test2(){
        // 创建httpclient客户端对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 构建get请求对象
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        // 解决发送请求时被认为不是真人行为
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36");
        // 添加防盗链
        httpGet.addHeader("Referer", "https://www.baidu.com/");
        // 构建响应对象
        CloseableHttpResponse response = null;
        try {
            // 发送请求
            response = httpClient.execute(httpGet);
            // 返回结果
            HttpEntity entity = response.getEntity();
            // 获取响应结果
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
            // 关闭流
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null){
                    response.close();
                }
                if (httpGet != null){
                    httpGet.releaseConnection();
                }
                if (httpClient!= null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // end
        return "success";
    }
  • 测试3: 发送有参get请求,参数中带特殊字符
    @GetMapping("/test3")
    @ResponseBody
    public String test3() throws UnsupportedEncodingException {
        // 创建httpclient客户端对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String passwordParam = "123+abc";
        // 设置参数的编码
        String password = URLEncoder.encode(passwordParam, StandardCharsets.UTF_8.name());
        String urlString = "http://localhost:8080/demo?password=" + password;
        // 构建get请求对象
        HttpGet httpGet = new HttpGet(urlString);
        // 解决发送请求时被认为不是真人行为
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36");
        // 构建响应对象
        CloseableHttpResponse response = null;
        try {
            // 发送请求
            response = httpClient.execute(httpGet);
            // 返回结果
            HttpEntity entity = response.getEntity();
            // 获取响应结果
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
            // 关闭流
            EntityUtils.consume(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null){
                    response.close();
                }
                if (httpGet != null){
                    httpGet.releaseConnection();
                }
                if (httpClient!= null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // end
        return "success";
    }
posted @ 2022-04-01 19:34  DogLeftover  阅读(30)  评论(0编辑  收藏  举报