HttpClient(4.3.5) - Redirect Handling

时间:2022-04-30 10:26:51

HttpClient handles all types of redirects automatically, except those explicitly prohibited by the HTTP specification as requiring user intervention. See Other (status code 303) redirects on POST and PUT requests are converted to GET requests as required by the HTTP specification. One can use a custom redirect strategy to relaxe restrictions on automatic redirection of POST methods imposed by the HTTP specification.

LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
CloseableHttpClient httpclient = HttpClients.custom()
.setRedirectStrategy(redirectStrategy)
.build();

HttpClient often has to rewrite the request message in the process of its execution. Per default HTTP/1.0 and HTTP/1.1 generally use relative request URIs. Likewise, original request may get redirected from location to another multiple times. The final interpreted absolute HTTP location can be built using the original request and the context. The utility method URIUtils#resolve can be used to build the interpreted absolute URI used to generate the final request. This method includes the last fragment identifier from the redirect requests or the original request.

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://localhost:8080/");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
// Expected to be an absolute URI
} finally {
response.close();
}

HttpClient(4.3.5) - Redirect Handling的更多相关文章

  1. HttpClient&lpar;4&period;3&period;5&rpar; - Exception Handling

    HttpClient can throw two types of exceptions: java.io.IOException in case of an I/O failure such as ...

  2. Table of Contents - HttpClient

    HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...

  3. httpclient4 文档翻译

    前言超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需要 ...

  4. httpcomponents-client-4&period;4&period;x

    Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...

  5. httpcomponents-client-ga&lpar;4&period;5&rpar;

    http://hc.apache.org/httpcomponents-client-ga/tutorial/html/   Chapter 1. Fundamentals Prev     Next ...

  6. httpcomponents-client-4&period;3&period;x DOC

    Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...

  7. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-006- 如何保持重定向的request数据&lpar;用model、占位符、RedirectAttributes、model&period;addFlashAttribute&lpar;&quot&semi;spitter&quot&semi;&comma; spitter&rpar;&semi;&rpar;

    一.redirect为什么会丢数据? when a handler method completes, any model data specified in the method is copied ...

  8. restTemplate重定向问题 &amp&semi;cookie问题

    最近在做一个转发功能,zuul + ribbon + resttemplate 进行路由.负载.转发的功能 基本准备就绪,在微信自动登陆那遇到了一个坑,ribbon 系统用resttemplate 转 ...

  9. TIMEOUT HANDLING WITH HTTPCLIENT

    https://www.thomaslevesque.com/2018/02/25/better-timeout-handling-with-httpclient/ The problem If yo ...

随机推荐

  1. 攻克Spring

    http://www.cnblogs.com/dream-to-pku/p/5655247.html

  2. 史上最&quot&semi;恐怖&quot&semi;的12生肖图&comma;绝对超猛

    史上最“恐怖”的十二生肖图,绝对超猛!图片依次是:鼠 牛 虎 兔 龙 蛇 马 羊 猴 鸡 狗 猪!

  3. HDU 4858 项目管理(邻接表 暴力模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可 ...

  4. (转)Eclipse快捷键大全&comma;导包快捷键&colon;ctrl&plus;Shift&plus;&sol;

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  5. HDU 1002 A &plus; B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  6. SSH学习

    简介 SSH或Secure Shell是一种远程管理协议,允许用户通过Internet控制和修改远程服务器.该服务是作为未加密Telnet的安全替代品创建的,它使用加密技术确保与远程服务器之间的所有通 ...

  7. Diverse Garland CodeForces - 1108D (贪心&plus;暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  8. 大数据学习笔记1-大数据处理架构Hadoop

    Hadoop:一个开源的.可运行于大规模集群上的分布式计算平台.实现了MapReduce计算模型和分布式文件系统HDFS等功能,方便用户轻松编写分布式并行程序. Hadoop生态系统: HDFS:Ha ...

  9. numpy 初识(三)

    基本运算 exp: e sqrt:开放 floor:向下取整 ravel:矩阵拉成一个向 T:转置(行和列变换) 改变形状: resize: 更改其形状(返回值为None)a.resize(6,2) ...

  10. Python 网络编程相关知识学习

    Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...