Java实现两个Ip相互跳转

需求:在请求HttpClient时,如果访问的http://127.0.0.1:5004/proxy/1为空或者为html(看自己的需求而定),那么就跳转到http://192.128.121.140:5004/proxy/1

传入的ip1和ip2分别是127.0.0.1和192.128.121.140

java 复制代码
private String sendRequest(String ip1, String ip2) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String urlTemplate = "http://%s/proxy/1";
    String resBody = sendRequestToIP(httpClient, String.format(urlTemplate, ip1));
    if (resBody.contains("html") || resBody.isEmpty()) {
        resBody = sendRequestToIP(httpClient, String.format(urlTemplate, ip2));
    }
    return resBody;
}
java 复制代码
private String sendRequestToIP(CloseableHttpClient httpClient, String url) {
    String resBody = "";
    try {
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(request);
        resBody = EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        log.error("Exception occurred while sending request to " + url);
    }
    return resBody;
}
相关推荐
heart000_14 分钟前
基于 WebWorker 的 WebAssembly 图像处理吞吐量分析
java·图像处理·wasm
菜鸟阿达7 分钟前
Idea 2025 commit 关闭侧边栏 开启探框
java·ide·intellij-idea
雨果talk11 分钟前
【一文看懂多模块Bean初始化难题】Spring Boot多模块项目中的Bean初始化难题:包名不一致的优雅解决方案
java·spring boot·后端·spring·springboot
Ai财富密码20 分钟前
【Python教程】CentOS系统下Miniconda3安装与Python项目后台运行全攻略
开发语言·python·numpy
xiaowu08027 分钟前
C# 中的Async 和 Await 的用法详解
java·开发语言·c#
范纹杉想快点毕业32 分钟前
初探Qt信号与槽机制
java·c语言·开发语言·c++·qt·visualstudio·visual studio
哈喽姥爷32 分钟前
苍穹外卖--缓存菜品Spring Cache
java·缓存·spring cache·苍穹外卖·黑马
stein_java1 小时前
springMVC-13 文件下载及上传
java·spring
容器( ु⁎ᴗ_ᴗ⁎)ु.。oO1 小时前
Magentic-ui项目相关整理
开发语言·javascript·ui
Cyanto1 小时前
Spring MVC 核心枢纽:DispatcherServlet 的深度解析与实践价值
java·spring·mvc