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;
}
相关推荐
忆源32 分钟前
【Qt】之音视频编程1:QtAV的背景和安装篇
开发语言·qt·音视频
敲键盘的小夜猫35 分钟前
Python核心数据类型全解析:字符串、列表、元组、字典与集合
开发语言·python
李匠202438 分钟前
C++GO语言微服务之图片、短信验证码生成及存储
开发语言·c++·微服务·golang
巨龙之路4 小时前
C语言中的assert
c语言·开发语言
2301_776681655 小时前
【用「概率思维」重新理解生活】
开发语言·人工智能·自然语言处理
熊大如如5 小时前
Java 反射
java·开发语言
猿来入此小猿5 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
ll7788115 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
我不想当小卡拉米6 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++
teacher伟大光荣且正确6 小时前
Qt Creator 配置 Android 编译环境
android·开发语言·qt