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

时间:2012-12-23 14:26:59

标签: 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;

任何提示都表示赞赏。

1 个答案:

答案 0 :(得分:5)

您可以使用TIdLogDebug作为您的IdHttp的拦截 事件OnSend和OnReceive将在数组或TBytes中提供所需的信息。