|
|
package com.utils;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
|
|
|
/**
|
|
|
* HTTP客户端工具类
|
|
|
*
|
|
|
* 核心职责:提供简单的HTTP GET请求功能
|
|
|
*
|
|
|
* 技术特点:
|
|
|
* 1. 基于Java原生HttpURLConnection实现
|
|
|
* 2. 支持UTF-8编码,避免中文乱码
|
|
|
* 3. 简单的异常处理机制
|
|
|
* 4. 轻量级,无第三方依赖
|
|
|
*
|
|
|
* 使用场景:
|
|
|
* - 调用第三方API接口
|
|
|
* - 获取远程数据
|
|
|
* - 简单的HTTP通信需求
|
|
|
*
|
|
|
* @author long.he01
|
|
|
*/
|
|
|
public class HttpClientUtils {
|
|
|
|
|
|
/**
|
|
|
* 执行HTTP GET请求
|
|
|
*
|
|
|
* @param uri 请求的URL地址,可以包含查询参数
|
|
|
* @return 服务器返回的响应内容,如果请求失败则返回null
|
|
|
* @description 使用GET方式发送HTTP请求,支持UTF-8编码
|
|
|
* @author: long.he01
|
|
|
*/
|
|
|
public static String doGet(String uri) {
|
|
|
// 使用StringBuilder构建响应内容(更高效)
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
try {
|
|
|
String res = "";
|
|
|
// 创建URL对象
|
|
|
URL url = new URL(uri);
|
|
|
|
|
|
// 打开HTTP连接
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
// 设置请求方法为GET
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
|
|
// 设置连接超时和读取超时时间(建议添加)
|
|
|
conn.setConnectTimeout(5000); // 5秒连接超时
|
|
|
conn.setReadTimeout(10000); // 10秒读取超时
|
|
|
|
|
|
// 设置请求头(可根据需要添加)
|
|
|
conn.setRequestProperty("Accept", "application/json");
|
|
|
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
|
|
|
|
|
|
// 获取响应码
|
|
|
int responseCode = conn.getResponseCode();
|
|
|
|
|
|
// 检查请求是否成功(200表示成功)
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
// 创建输入流读取器,使用UTF-8编码避免中文乱码
|
|
|
BufferedReader in = new BufferedReader(
|
|
|
new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
|
|
|
String line;
|
|
|
// 逐行读取响应内容
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
// 将每行内容添加到结果中,并添加换行符
|
|
|
res += line + "\n";
|
|
|
}
|
|
|
|
|
|
// 关闭输入流
|
|
|
in.close();
|
|
|
|
|
|
return res;
|
|
|
} else {
|
|
|
// 请求失败,记录错误信息
|
|
|
System.err.println("HTTP GET请求失败,响应码: " + responseCode);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
// 打印异常堆栈信息
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 可以添加的增强方法:
|
|
|
|
|
|
/**
|
|
|
* 增强版GET请求,支持自定义超时时间
|
|
|
*
|
|
|
* @param uri 请求URL
|
|
|
* @param connectTimeout 连接超时时间(毫秒)
|
|
|
* @param readTimeout 读取超时时间(毫秒)
|
|
|
* @return 响应内容
|
|
|
*/
|
|
|
public static String doGet(String uri, int connectTimeout, int readTimeout) {
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
try {
|
|
|
URL url = new URL(uri);
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
conn.setRequestMethod("GET");
|
|
|
conn.setConnectTimeout(connectTimeout);
|
|
|
conn.setReadTimeout(readTimeout);
|
|
|
|
|
|
// 设置通用请求头
|
|
|
conn.setRequestProperty("Accept", "*/*");
|
|
|
conn.setRequestProperty("Connection", "keep-alive");
|
|
|
conn.setRequestProperty("User-Agent",
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
|
|
|
|
|
int responseCode = conn.getResponseCode();
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
BufferedReader in = new BufferedReader(
|
|
|
new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
|
|
|
String line;
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
result.append(line).append("\n");
|
|
|
}
|
|
|
in.close();
|
|
|
|
|
|
return result.toString();
|
|
|
} else {
|
|
|
System.err.println("HTTP请求失败,响应码: " + responseCode);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 带重试机制的GET请求
|
|
|
*
|
|
|
* @param uri 请求URL
|
|
|
* @param retryTimes 重试次数
|
|
|
* @return 响应内容
|
|
|
*/
|
|
|
public static String doGetWithRetry(String uri, int retryTimes) {
|
|
|
for (int i = 0; i < retryTimes; i++) {
|
|
|
try {
|
|
|
String result = doGet(uri);
|
|
|
if (result != null) {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 等待一段时间后重试
|
|
|
if (i < retryTimes - 1) {
|
|
|
Thread.sleep(1000 * (i + 1)); // 递增等待时间
|
|
|
}
|
|
|
} catch (InterruptedException ie) {
|
|
|
Thread.currentThread().interrupt();
|
|
|
break;
|
|
|
} catch (Exception e) {
|
|
|
// 记录日志,继续重试
|
|
|
System.err.println("第" + (i + 1) + "次请求失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
} |