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;
}
相关推荐
华仔啊2 分钟前
Java 开发必看:什么时候用 for,什么时候用 Stream?
java·后端
未来之窗软件服务3 分钟前
幽冥大陆(五十八)php1024位密码生成—东方仙盟筑基期
开发语言·算法·仙盟创梦ide·东方仙盟
刺客xs19 分钟前
Qt ----- QT线程
开发语言·qt
夏幻灵32 分钟前
C++ 里 什么时候不用指针,而选择值拷贝/深拷贝 ?
开发语言·c++·算法
yong999037 分钟前
基于小波分析与粒子群算法的电网潮流优化实现(MATLAB)
开发语言·算法·matlab
superman超哥38 分钟前
仓颉语言中字典的增删改查:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
tgethe39 分钟前
Java 数组(Array)笔记:从语法到 JVM 内核
java·数据结构
红牛203044 分钟前
Nexus Repository搭建maven远程仓库
java·maven·nexus
又是忙碌的一天1 小时前
Maven基本概念
java·maven
@淡 定1 小时前
JVM内存区域划分详解
java·jvm·算法