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;
}
相关推荐
Java中文社群1 分钟前
26届双非上岸记!快手之战~
java·后端·面试
whitepure6 分钟前
万字详解Java中的面向对象(二)——设计模式
java·设计模式
whitepure8 分钟前
万字详解Java中的面向对象(一)——设计原则
java·后端
后台开发者Ethan26 分钟前
Python需要了解的一些知识
开发语言·人工智能·python
2301_7930868735 分钟前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud
回家路上绕了弯43 分钟前
MySQL 详细使用指南:从入门到精通
java·mysql
小七rrrrr1 小时前
动态规划法 - 53. 最大子数组和
java·算法·动态规划
自由的疯1 小时前
在 Java IDEA 中使用 DeepSeek 详解
java·后端·架构
自由的疯1 小时前
Java 通过接口方式使用 DeepSeek 详解
java·后端·trae