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;
}
相关推荐
憨憨崽&6 分钟前
进击大厂:程序员必须修炼的算法“内功”与思维体系
开发语言·数据结构·算法·链表·贪心算法·线性回归·动态规划
7***684310 分钟前
Spring Boot 热部署
java·spring boot·后端
毕设源码-邱学长12 分钟前
【开题答辩全过程】以 基于Java的公职备考在线学习系统的设计与实现为例,包含答辩的问题和答案
java·开发语言·学习
二川bro1 小时前
模型部署实战:Python结合ONNX与TensorRT
开发语言·python
联系QQ:276998851 小时前
电化学与冷启动仿真的赝电容计算及GITT扩散系数研究——阻抗分析拟合与全电池电容器性能评估
开发语言
Croa-vo1 小时前
Tesla Spring 2026 Co-op 面经:CodeSignal真题解析与通关攻略
java·后端·spring
言言的底层世界1 小时前
c++中STL容器及算法等
开发语言·c++·经验分享·笔记
C***u1761 小时前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
CodeAmaz1 小时前
Spring 事务失效的 8 种常见场景总结
java·spring·事务失效
liu****2 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法