当前位置: 首页 > article >正文

使用HttpsClient来idea发送请求

1. 依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>            
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

2.使用

package com.sky;

import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Slf4j
@SpringBootTest
public class TestHttpClient {

    @Test
    public void doGet () throws IOException {
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建HttpGet对象
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        //3.使用HttpClient发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.解析响应
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        //5.获取响应体
        HttpEntity entity = response.getEntity();
        System.out.println(entity);
        //6.获取响应内容
        String string = EntityUtils.toString(entity);
        System.out.println(string);
        //6.关闭资源
        httpClient.close();
        response.close();
    }


    @Test
    public void doPost() throws JSONException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

        //设置请求体
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);

        //发起请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = " + statusCode);

        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity);
        System.out.println("string = " + string);

    }

}


http://www.kler.cn/news/341053.html

相关文章:

  • Android targetSdkVersion 升级为34 问题处理
  • 减少重复的请求之promise缓存池(闭包版) —— 缓存promise,多次promise等待并返回第一个promise的结果
  • 75.【C语言】文件操作(2)
  • 计算机毕业设计—基于python技术的机器学习、深度学习毕业设计选题的一些思考及参考
  • Pikachu-File Inclusion-远程文件包含
  • redis——哨兵机制
  • Vortex GPGPU的github流程跑通与功能模块波形探索(二)
  • 环形缓冲区(Ring Buffer)在STM32 HAL库中的应用:防止按键丢失
  • 第69期 | GPTSecurity周报
  • 三色标记算法
  • prctl的函数和pthread_self函数
  • Linux中的多线程
  • 打造直播美颜平台的关键技术:视频美颜SDK的深度解析
  • QDesktopWidget Class
  • Chromium 中js Fetch API接口c++代码实现(一)
  • HarmonyOS第一课 05 从简单的页面开始-开发01
  • Dockerfile搭建环境案例
  • 【EXCEL数据处理】000021 案例 保姆级教程,附多个操作案例。EXCEL文档安全性设置。
  • 「Ubuntu」文件权限说明(drwxr-xr-x)
  • LabVIEW提高开发效率技巧----阻塞时钟