如何使用HttpClient处理post方法中的重定向?

发布于 2024-11-30 08:13:25 字数 202 浏览 1 评论 0原文

在我的应用程序中,我将 xml 文件 POST 到服务器,但有时服务器会发回 302,然后重定向。

但是,重定向后方法变为 GET ,而不是 POST ,并且我的 xml 文件中的数据无法传递到服务器。

最后我得到的状态代码是404。

有什么方法可以自己处理重定向吗?当重定向发生时我可以做些什么吗?

有人可以帮忙吗?谢谢。!

In My App , I POST a xml file to the server , but sometimes the server will send back 302 and then redirect .

However , after the redirecting the method become GET , not POST, and my data in xml file can't deliver to server.

And finally the status code I got is 404.

Is there some way to process the redirect by myself ? Can I do something when the redirecting is happening?

Anyone can help? THX.!

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

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

发布评论

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

评论(2

浅笑轻吟梦一曲 2024-12-07 08:13:25

来自 RFC 2616

如果收到 302 状态代码是为了响应除
GET 或 HEAD,用户代理不得自动重定向
请求,除非它可以被用户确认,因为这可能
更改发出请求的条件。

您确定您的服务器没有使用“POST、重定向、GET”成语?

您可以在 Apache 中禁用自动跟踪重定向 HTTP 客户端。例如:

_httpClient = new DefaultHttpClient();
_httpClient.getParams().setParameter(
    ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);

From RFC 2616:

If the 302 status code is received in response to a request other than
GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.

Are you sure your server isn't using the 'POST, redirect, GET' idiom?

You can disable automatic following of redirects in Apache HTTP client. For example:

_httpClient = new DefaultHttpClient();
_httpClient.getParams().setParameter(
    ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
相守太难 2024-12-07 08:13:25

根据记录,302 不应被视为执行 GET 重定向请求的指令,而这正是 303 的用途。这种行为是草率的,HttpClient 的开发人员应该为自己感到羞耻... :-P

我怀疑你唯一的选择是编写你自己的 HTTP 客户端的套接字级实现,以正确处理重定向。您可以在互联网上浏览一下,看看是否有其他人已经生成了一些您可以借用的代码,但如果没有,您将不得不自己制作。

我以前在 PHP 中做过这个 - 实际上并不那么难,HTTP 是一个实现起来相当简单的协议。我认为 cURL 可以正确处理这些重定向,如果您可以将任务分给它?

For the record, 302's should not be treated as an instruction to perform the redirected request as a GET, that is what 303's are for. This behaviour is sloppy and the developers of HttpClient should be ashamed of themselves... :-P

I suspect your only option would be write your own, socket level implementation of an HTTP Client, that handles the redirects properly. You can have a look round the internet to see if anyone else has already produced to some code you can borrow, but if not you will have to do it yourself.

I have done this before in PHP - it's not actually that hard, HTTP is a fairly simple protocol to implement. I think cURL handles these redirects properly, if you can sub the task out to that?

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