parent
5edba550fe
commit
66e4a4424d
@ -0,0 +1,29 @@
|
||||
package com.utils; // 声明工具类所在包
|
||||
|
||||
import java.io.BufferedReader; // 导入缓冲字符输入流
|
||||
import java.io.InputStreamReader; // 导入字节流转字符流
|
||||
import java.net.HttpURLConnection; // 导入HTTP连接类
|
||||
import java.net.URL; // 导入URL处理类
|
||||
|
||||
public class HttpClientUtils { // HTTP客户端工具类
|
||||
|
||||
public static String doGet(String uri) { // GET请求方法
|
||||
StringBuilder result = new StringBuilder(); // 创建结果字符串构建器
|
||||
try {
|
||||
String res = ""; // 初始化响应字符串
|
||||
URL url = new URL(uri); // 创建URL对象
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 打开HTTP连接
|
||||
BufferedReader in = new BufferedReader( // 创建缓冲读取器
|
||||
new InputStreamReader(conn.getInputStream(), "UTF-8")); // 使用UTF-8编码读取输入流
|
||||
String line; // 声明行字符串变量
|
||||
while ((line = in.readLine()) != null) { // 逐行读取响应内容
|
||||
res += line+"\n"; // 拼接每行内容并换行
|
||||
}
|
||||
in.close(); // 关闭输入流
|
||||
return res; // 返回响应结果
|
||||
} catch (Exception e) { // 异常处理
|
||||
e.printStackTrace(); // 打印异常堆栈
|
||||
return null; // 返回空值
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue