gpt4 book ai didi

java - 在检索重定向 url 之前使用 Apache HttpHead 进行 URI 编码

转载 作者:行者123 更新时间:2023-12-01 12:25:08 25 4
gpt4 key购买 nike

我必须获取某些短网址的重定向网址,以检查是否可以下载最终页面。HttpHead 用于获取最终目标 url(参见下面的代码)。

HttpClient client = HttpClientBuilder.create().build();

HttpHead httpHead = new HttpHead("http://bit.ly/1DfSWSs");

HttpClientContext context = HttpClientContext.create();
client.execute(httpHead, context);
List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null && !redirectLocations.isEmpty()) {
for (URI redirectURI : redirectLocations) {
//TODO print all
}
}

问题出在代码中的 URL 示例。重定向 url 中包含空格,因此无法创建 URI。

在调用 getRedirectLocations 方法之前,有什么方法可以对 URI 对象进行编码吗?

最佳答案

自定义重定向策略是您的 friend

CloseableHttpClient client = HttpClientBuilder.create()
.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
protected URI createLocationURI(final String location) throws ProtocolException {
// One can try to rewrite malformed redirect locations
//at this point
return super.createLocationURI(location);
}
})
.build();

关于java - 在检索重定向 url 之前使用 Apache HttpHead 进行 URI 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421193/

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