首页 > 解决方案 > 如何在 AsyncHttpClient 中停止重定向并获得响应

问题描述

我有一个HTTP GET返回状态200和一些响应。此响应是重定向的结果。

如果我asyncHttpClient.setEnableRedirects(false);在我的代码中引入,那么重定向停止并且它以状态 302 失败。但是在我的应用程序中,这个状态 302 和与之关联的响应是我需要的。

我正在网上搜索并试图弄清楚,但我是 Java 新手,所以无法理解如何实现这一点。

我想要的是,当服务器返回状态 302 时,我想触发 onSuccess 并捕获响应。谢谢。

标签: javaxmlhttprequesthttp-gethttp-redirecthttp-status-code-302

解决方案


If I'm getting right what do you need then you could implement your own AsyncHandlers to react with a different AsyncHandler.State for 302 status code. ( https://github.com/AsyncHttpClient/async-http-client#using-custom-asynchandlers) or while creating the client stop to follow redirects like this:

asyncHttpClient(config().setFollowRedirect(false))

(it might be different from the approach you mentioned above.)

P.S. I'm using 2.5.2 version of the asynchttpclient lib.


推荐阅读