一.发送POST方式请求
编写代码:
1.创建一个HttpClient对象
2.创建一个HttpGet请求
3.发送http的get请求并获得响应对象
4.通过发送GET请求获取的CloseableHttpResponse响应对象来获取状态码以及响应数据
            
            
              java
              
              
            
          
          package com.sky.test;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.HttpUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.UnsupportedEncodingException;
@SpringBootTest
public class HttpClientTest {
    /**
      测试以代码方式发送httpGet请求
     */
    @Test
    public void testGet() throws Exception{
        // 1.创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.创建一个HttpGet请求
        HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
        // 3.发送http的get请求并获得响应对象
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4.通过发送GET请求获取的CloseableHttpResponse响应对象来获取状态码以及响应数据
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("服务端返回的状态码为:"+statusCode);
        HttpEntity entity = response.getEntity();
        String body = EntityUtils.toString(entity);     // 响应体
        System.out.println("服务端返回的数据为:" + body);
        // 关闭资源
        response.close();
        httpClient.close();
    }
    /**
        测试以代码方式发送httpPost请求
     */
    @Test
    public void testPost() throws Exception {
        // 1.创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2.创建一个HttpPost请求
        HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
        JSONObject jsonObject = new JSONObject();   //  使用fastJSON包下的JSONObject对象来创建一个json格式的数据
        jsonObject.put("username","admin");
        jsonObject.put("password","123456");
        // 调用toString()方法将JSON格式的数据转为字符串
        StringEntity entity = new StringEntity(jsonObject.toString());   // StringEntity是HttpEntity的实现类,其构造方法要传入一个字符串,生成StringEntity类型的对象
        // 设置请求的编码方式为utf-8
        entity.setContentEncoding("utf-8");
        // 设置请求的数据格式为JSON
        entity.setContentType("application/json");
        // post请求要设置请求参数,为entity
        httpPost.setEntity(entity);     // setEntity()方法中要传入一个HttpEntity对象
        // 3.发送http的post请求并获得响应对象
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        // 4.通过发送POST请求获取的CloseableHttpResponse响应对象来获取状态码以及响应数据
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("服务器返回的状态码为:" + statusCode);
        HttpEntity entity1 = httpResponse.getEntity();
        String body = EntityUtils.toString(entity1);    // 响应体
        System.out.println("服务器响应数据为" + body);
        // 关闭资源
        httpResponse.close();
        httpClient.close();
    }
} 请注意:运行时一定要保证项目启动和redis启动。
请注意:运行时一定要保证项目启动和redis启动。