Android 重定向(返回代码 302)处理

发布于 2024-10-08 01:23:10 字数 445 浏览 4 评论 0原文

我正在使用 HttpClient 对服务器 URL 执行 Post 请求。服务器 URL 正在重定向我,并且似乎我的 HttpClient 正在自动重定向。我想拦截重定向响应 302。我无法使用 setFollowRedirects(false)。我想知道是否有办法拦截 302 响应并停止自动重定向?我看到我们可以使用 HttpURL Connection 来实现这一点

   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.setRequestMethod("POST");
   connection.setFollowRedirects(false);

,但是我想使用 HttpClient 和 HttpPost 来避免当前实现中的巨大变化。有什么办法可以达到同样的目的吗?

I am using HttpClient to do Post request to a server URL. The server URL is redirecting me and seems like my HttpClient is getting redirected automatically. I wanted to intercept the redirect response 302. I am unable to use setFollowRedirects(false). I wanted to know if there is anyway to do intercept the 302 response and stop Auto redirection?? I saw that we can achieve this using HttpURL Connection

   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.setRequestMethod("POST");
   connection.setFollowRedirects(false);

However I want to use HttpClient and HttpPost to avoid huge changes in my current implementation. Is there any way to achieve the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-10-15 01:23:10

ResponseHandler.handleResponse(HttpResponse response)HttpClient 类的一部分。

此外,大多数 HttpClient.execute 方法都会返回 HttpResponse。使用 HttpResponse.getStatusLine().getStatusCode() 。

另请参阅org.apache.http.client.RedirectHandler

ResponseHandler.handleResponse(HttpResponse response) is part of the HttpClient class.

Also most of the HttpClient.execute methods return an HttpResponse. Use HttpResponse.getStatusLine().getStatusCode().

Also look at org.apache.http.client.RedirectHandler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文