简体   繁体   English

在POST重定向后,Indy使用GET发送params

[英]Indy sends params with GET after POST redirect

Using Delphi XE5 + Indy 10. 使用Delphi XE5 + Indy 10。

I am sending POST with login and password to log in. Site responds with redirect (302) to target page. 我发送登录名和密码POST以登录。站点响应重定向(302)到目标页面。 In browser, redirect is handled by GET and everything goes right, but Indy continues with POST. 在浏览器中,重定向由GET处理,一切正常,但Indy继续POST。

I solve this by using this code inside my OnRedirect handler: 我通过在我的OnRedirect处理程序中使用此代码来解决此问题:

procedure TForm1.MyRedirect(Sender: TObject;
                            var dest: string;
                            var NumRedirect: Integer;
                            var Handled: Boolean;
                            var VMethod: string);
var
  TempHttp: TIdHttp;
begin
  TempHttp := (Sender as TIdHTTP);

  if (TempHttp.ResponseCode = 302) then
    VMethod := 'GET';

  Handled := true;
end;

Request method is then changed to GET, but Indy still sends POST request params with GET. 请求方法然后更改为GET,但Indy仍然使用GET发送POST请求参数。 So I get 413 Request Entity Too Large response. 所以我得到413请求实体太大的响应。

How can I make Indy NOT send params with GET after redirect? 我怎样才能让Indy在重定向后不用GET发送params? Solution inside OnRedirect would be ideal. OnRedirect内部的解决方案将是理想的。

Thanks! 谢谢!

Client behavior for handling the HTTP 302 reply code is ambiguous, and often treated erroneously by various clients. 处理HTTP 302回复代码的客户端行为是不明确的,并且经常被各种客户端错误地处理。 This is well documented in various RFCs, including 2068 and 2616. The 303 reply code was created to resolve the ambiguity, but many clients still do not support 303 yet, and many servers still use 302 expecting clients to behave as if 303 was used. 这已在各种RFC中得到充分记录,包括2068和2616. 303回复代码是为了解决歧义而创建的,但许多客户端仍然不支持303 ,并且许多服务器仍然使用302期望客户端表现得好像使用了303

TIdHTTP has jumped back and forth many times over the years trying to figure out what behavior should be used when 302 is received - should it redirect using GET , or should it redirect using POST ? TIdHTTP多次来回试图弄清楚在收到302时应该使用什么行为 - 它应该使用GET重定向,还是应该使用POST重定向? In 2012, an hoTreat302Like303 flag was added to the TIdHTTP.HTTPOptions property to let users decide what to do. 2012年,在TIdHTTP.HTTPOptions属性中添加了hoTreat302Like303标志,以便用户决定该怎么做。 So make sure you are using an up-to-date version of Indy. 因此,请确保您使用的是最新版本的Indy。

If 303 is received, TIdHTTP will clear its Request.Source property (thus ignoring any previous POST params) and send a GET request, ignoring the method returned by the OnRedirect event handler, if assigned. 如果收到303TIdHTTP将清除其Request.Source属性(从而忽略任何以前的POST参数)并发送GET请求,忽略OnRedirect事件处理程序返回的方法(如果已分配)。

If 302 is received: 如果收到302

  1. if hoTreat302Like303 is enabled, TIdHTTP will clear its Request.Source property (thus ignoring any previous POST params) and send a GET request, ignoring the method returned by the OnRedirect event handler, if assigned. 如果hoTreat302Like303启用, TIdHTTP将清除它Request.Source财产(从而忽视了以往任何POST PARAMS),并发送一个GET请求,无视返回的方法OnRedirect事件处理程序,如果分配。

  2. if hoTreat302Like303 is disabled (which it is by default), TIdHTTP will send a request using the method returned by the OnRedirect event handler if assigned, otherwise it will send a request using the same method as the previous request that was redirected. 如果禁用了hoTreat302Like303 (默认情况下), TIdHTTP将使用OnRedirect事件处理程序返回的方法发送请求(如果已分配),否则它将使用与重定向的上一个请求相同的方法发送请求。 But in either case, it does not clear its Request.Source property (thus any previous POST params will be re-sent). 但在任何一种情况下,它都不会清除它的Request.Source属性(因此将重新发送任何以前的POST参数)。 So if you change the method in the OnRedirect handler, you will have to update the Request.Source property accordingly as well, eg: 因此,如果您更改OnRedirect处理程序中的方法,则必须相应地更新Request.Source属性,例如:

     procedure TForm1.MyRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: Boolean; var VMethod: string); var TempHttp: TIdHttp; begin TempHttp := (Sender as TIdHTTP); if (TempHttp.ResponseCode = 302) then begin VMethod := 'GET'; TempHttp.Request.Source := nil; // <-- add this end; Handled := true; end; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM