- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须获取某些短网址的重定向网址,以检查是否可以下载最终页面。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/
我们使用 HttpHead 从客户的网站获取信息,但由于某种原因,我们也在响应中获取 cookie。是预期的吗?有没有办法设置不返回cookie? 下面是我们的代码 HttpClien
在我的 ASP.NET 核心 Controller 中,我有以下 HttpGet 函数: [HttpGet("[action]")] [HttpHead("[action]")] [ResponseC
我遇到一个问题,我正在触发 http 请求来获取这样的 header 。我的目标是检查当我触发请求时是否会得到响应。 try { HttpHead headReq = new
我们有以下代码,后来用 HttpHead 方法替换,因为我们只需要拉回网页的 header 信息。更改后,我们注意到,平均而言,对于同一组网页,HttpHead 的返回时间比 HttpGet 的返回时
我必须获取某些短网址的重定向网址,以检查是否可以下载最终页面。HttpHead 用于获取最终目标 url(参见下面的代码)。 HttpClient client = HttpClientBuilder
使用下面的测试类,/////1///// 处的行抛出一个 NoHttpResponseException(请参阅末尾的日志记录异常跟踪问题)。执行任何其他行 /////2-to-8///// 可以很好
我是一名优秀的程序员,十分优秀!