wordpress 企业网站制作,外贸网站怎么换域名,广州品牌,专业的网页设计和网站建设公司文章目录 1. HttpClient 介绍2. 导入坐标3. 使用 HttpClient 发送 Get 请求4. 使用 HttpClient 发送 Post 请求 1. HttpClient 介绍
HttpClient 是 Apache Jakarta Common下的子项目#xff0c;用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包#xff… 文章目录 1. HttpClient 介绍2. 导入坐标3. 使用 HttpClient 发送 Get 请求4. 使用 HttpClient 发送 Post 请求 1. HttpClient 介绍
HttpClient 是 Apache Jakarta Common下的子项目用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中比如 Apache Jakarta上很著名的另外两个开源项目 Cactus 和HTMLUnit 都使用了 HttpClient。
核心 API
HttpClientHttpClientsCloseableHttpClientHttpGetHttpPost
发送请求步骤
创建 HttpClient 对象创建 Http 请求对象调用 HttpClient 的 execute 方法发送请求
2. 导入坐标
dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactId
/dependency3. 使用 HttpClient 发送 Get 请求
package com.sky.test;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.IOException;SpringBootTest
public class HttpClientTest {/*** 测试httpclient GET*/Testpublic void testGet() throws IOException {// 创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();// 创建请求对象HttpGet httpGet new HttpGet(http://localhost:8080/user/shop/status);// 发送请求接收响应CloseableHttpResponse response httpClient.execute(httpGet);// 获取状态码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();}
}4. 使用 HttpClient 发送 Post 请求
package com.sky.test;import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.IOException;SpringBootTest
public class HttpClientTest {/*** 测试httpclient POST*/Testpublic void testPost() throws IOException {// 创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();// 创建请求对象HttpPost httpPost new HttpPost(http://localhost:8080/admin/employee/login);// 封装post请求JSONObject jsonObject new JSONObject();jsonObject.put(username, admin);jsonObject.put(password, 123456);StringEntity entity new StringEntity(jsonObject.toString());// 指定请求编码方式entity.setContentEncoding(utf-8);// 数据格式entity.setContentType(application/json);httpPost.setEntity(entity);// 发送请求接收响应CloseableHttpResponse response httpClient.execute(httpPost);// 获取状态码int statusCode response.getStatusLine().getStatusCode();System.out.println(响应码为: statusCode);// 获取响应数据HttpEntity entity1 response.getEntity();String body EntityUtils.toString(entity1);System.out.println(响应数据 body);// 关闭资源response.close();httpClient.close();}}