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;
}
相关推荐
罗曼蒂克在消亡10 分钟前
GraphQL规范
开发语言·graphql
HealthScience13 分钟前
怎么将bash(sh)的所有输出保存到log/txt中?
开发语言·bash
Word码38 分钟前
数据结构:栈和队列
c语言·开发语言·数据结构·经验分享·笔记·算法
五花肉村长42 分钟前
数据结构-队列
c语言·开发语言·数据结构·算法·visualstudio·编辑器
秋落风声42 分钟前
【数据结构】---图
java·数据结构··graph
萧鼎43 分钟前
Python常见问题解答:从基础到进阶
开发语言·python·ajax
2401_8576226644 分钟前
Spring Boot新闻推荐系统:性能优化策略
java·spring boot·后端
C1 小时前
C++_智能指针详解
开发语言·c++
想躺平的做题家1 小时前
Linux高级编程_29_信号
开发语言·c·信号
qinzechen1 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#