一.发送POST方式请求
编写代码:
1.创建一个HttpClient对象
2.创建一个HttpGet请求
3.发送http的get请求并获得响应对象
4.通过发送GET请求获取的CloseableHttpResponse响应对象来获取状态码以及响应数据
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请求*/@Testpublic 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请求*/@Testpublic 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-8entity.setContentEncoding("utf-8");// 设置请求的数据格式为JSONentity.setContentType("application/json");// post请求要设置请求参数,为entityhttpPost.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();}
}
首先创建一个HttpClient对象,CloseableHttpClient是HttpClient的一个实现类。然后我们创建一个HttpPost请求对象,并传入HttpPost请求的请求路径。
这里注意,我们使用HttpPost创建POST请求对象时,因为该POST请求要传入一个JSON格式的数据,封装用户名和密码。因此我们要设置请求参数,使用setEntity来设置请求参数entity。那么entity对象是怎么来的呢?首先使用JSONObject对象来创建一个json对象并为其赋值,然后将其转为字符串传入StringEntity的构造器中生成entity对象。并且指定entity对象的编码格式为utf-8,数据格式为json。
然后调用HttpClient对象的execute方法将HttpPost请求对象传入,发送http的get请求并获得响应对象。最后通过发送POST请求获取的CloseableHttpResponse响应对象来获取状态码以及响应数据。调用响应对象的.getStatusLine().getStatusCode()来获取响应状态码,然后调用getEntity()方法获取HttpEntity类型的响应体并转为String字符串。最后将响应对象和HttpClient对象关闭。
成功
请注意:运行时一定要保证项目启动和redis启动。