如何使用HttpClient处理post方法中的重定向?
在我的应用程序中,我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 RFC 2616:
您确定您的服务器没有使用“POST、重定向、GET”成语?
您可以在 Apache 中禁用自动跟踪重定向 HTTP 客户端。例如:
From RFC 2616:
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:
根据记录,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?