如何跟踪TIdHttp的原始请求和响应内容

sta*_*005 7 delphi debugging indy idhttp

我使用delphi和python实现了相同的代码(发布表单).python代码运行完美,但delphi代码失败.在python中,我可以简单地写httplib2.debuglevel=4一下,看看实际发送到服务器的内容是什么.但我不知道如何在delphi中打印内容.

def python_request_data(url, cookie, data):
    httplib2.debuglevel = 4
    conn = httplib2.Http()
    conn.follow_all_redirects = True
    headers = {'Cookie': cookie, 'Content-Type': 'application/x-www-form-urlencoded'}
    response, contents = conn.request(url, 'POST', data, headers=headers)

procedure DelphiRequestData(const Url, Cookie, Data: string);
var
  Client: TIdHttp;
  Params: TStringList;
  Response: string;
begin
  Client := TIdHttp.Create(nil);
  try
    Client.HTTPOptions := [hoKeepOrigProtocol];
    Client.Request.CustomHeaders.AddValue('Cookie', Cookie);
    Params := TStringList.Create;
    try
      Params.QuoteChar := #0;
      Params.Delimiter := '&';
      Params.DelimiterText := Data;
      Client.Request.ContentType := 'application/x-www-form-urlencoded';
      Client.Request.ContentLength := Length(Params.DelimitedText);
      Response := Client.Post(Url, Params);
    finally
      Params.Free;
    end;
  finally
    Client.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

任何提示都表示赞赏.

bum*_*mmi 5

您可以使用TIdLogDebug作为您的IdHttp的拦截。
事件OnSend和OnReceive将以数组或TByte的形式传递所需的信息。

  • TIdLogDebug`将其数据直接输出到调试器事件日志。如果要用自己的代码捕获数据,请改用`TIdLogEvent`。它的OnSent和OnReceived事件以String值而不是TBytes值的形式提供数据(您正在考虑TIdLogBase的底层Send()和Receive()方法)。 。 (4认同)