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;
}
相关推荐
daidaidaiyu8 分钟前
Spring IOC 源码学习 声明式事务的入口点
java·spring
myloveasuka21 分钟前
[Java]查找算法&排序算法
java·算法·排序算法
清水白石00829 分钟前
Free-Threaded Python 实战指南:机遇、风险与 PoC 验证方案
java·python·算法
wWYy.33 分钟前
STL:list
开发语言·c++
TON_G-T42 分钟前
day.js和 Moment.js
开发语言·javascript·ecmascript
发际线还在1 小时前
互联网大厂Java三轮面试全流程实战问答与解析
java·数据库·分布式·面试·并发·系统设计·大厂
飞Link1 小时前
具身智能核心架构之 Python 行为树 (py_trees) 深度剖析与实战
开发语言·人工智能·python·架构
_周游1 小时前
Kaptcha—Google验证码工具
java·intellij-idea·jquery
我真会写代码1 小时前
深入理解JVM GC:触发机制、OOM关联及核心垃圾回收算法
java·jvm·架构
本喵是FW1 小时前
C语言手记1
java·c语言·算法