gpt4 book ai didi

java - 阻止 Apache HttpClient 4 访问特定路由

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:35 25 4
gpt4 key购买 nike

向 HttpClient 提供应阻止访问的路由、主机名、IP 地址列表的最简单方法是什么?

像这样(这段代码不起作用,你现在不能设置0):

HttpHost block = new HttpHost("example.com", 80);
cm.setMaxPerRoute(new HttpRoute(block), 0);

最佳答案

final Set<HttpHost> sitesToBlock = new HashSet<HttpHost>();
sitesToBlock.add(new HttpHost("example.com", 80));

DefaultRoutePlanner routePlanner = new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) {

@Override
public HttpRoute determineRoute(
final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {

final HttpRoute route = super.determineRoute(host, request, context);
if (sitesToBlock.contains(route.getTargetHost())) {
throw new HttpException("Connection to " + host + " is blocked");
}
return route;
}
};

CloseableHttpClient client = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();

}

关于java - 阻止 Apache HttpClient 4 访问特定路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com