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;
}
相关推荐
坐吃山猪4 小时前
SpringBoot01-配置文件
java·开发语言
晚风(●•σ )5 小时前
C++语言程序设计——06 字符串
开发语言·c++
我叫汪枫5 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
Nicole-----5 小时前
Python - Union联合类型注解
开发语言·python
晚云与城5 小时前
今日分享:C++ -- list 容器
开发语言·c++
yaoxtao5 小时前
java.nio.file.InvalidPathException异常
java·linux·ubuntu
兰雪簪轩5 小时前
分布式通信平台测试报告
开发语言·网络·c++·网络协议·测试报告
FPGAI6 小时前
Qt编程之信号与槽
开发语言·qt
Swift社区6 小时前
从 JDK 1.8 切换到 JDK 21 时遇到 NoProviderFoundException 该如何解决?
java·开发语言
0wioiw07 小时前
Go基础(④指针)
开发语言·后端·golang