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;
}
相关推荐
某空m1 分钟前
【Android】BottomNavigationView实现底部导航栏
android·java
顾漂亮4 分钟前
Spring AOP 实战案例+避坑指南
java·后端·spring
科比不来it23 分钟前
Go语言数据竞争Data Race 问题怎么检测?怎么解决?
开发语言·c++·golang
biter down26 分钟前
c语言14:字符指针
c语言·开发语言
SimonKing34 分钟前
Mybatis-Plus的竞争对手来了,试试 MyBatis-Flex
java·后端·程序员
光军oi40 分钟前
JAVA全栈JVM篇————初识JVM
java·开发语言·jvm
我命由我123451 小时前
PDFBox - PDFBox 加载 PDF 异常清单(数据为 null、数据为空、数据异常、文件为 null、文件不存在、文件异常)
java·服务器·后端·java-ee·pdf·intellij-idea·intellij idea
7哥♡ۣۖᝰꫛꫀꪝۣℋ1 小时前
Spring Boot
java·spring boot·后端
Moniane1 小时前
C++深度解析:从核心特性到现代编程实践
java·开发语言·jvm
uxiang_blog1 小时前
C++进阶:重载类型转换
linux·开发语言·c++