展开
拓展 关闭
订阅号推广码
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 @   DogLeftover  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示