How to trace raw request and response content of TIdHttp
Asked Answered
S

1

7

I implemented same code (to post a form) using delphi and python. The python code works perfectly, but delphi code fails. In python, I can simply write httplib2.debuglevel=4 to see what content has actually been sent to the server. but I have no idea how to print the content in 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;

Any hints are appreciated.

Snowber answered 23/12, 2012 at 14:26 Comment(1)
Thanks for the hints of @bummi. After seeing the log, I saw TIdHttp sends incorrect protocol version, unless I set Client.HTTPOptions := [hoKeepOrigProtocol].Snowber
C
6

You ca use TIdLogDebug as Intercept of your IdHttp.
The Events OnSend and OnReceive will deliver the desired Informations in a Array or TBytes.

Casa answered 23/12, 2012 at 15:13 Comment(1)
TIdLogDebug outputs its data directly to the debugger event log. If you want to capture the data in your own code, use TIdLogEvent instead. Its OnSent and OnReceived events provide the data as String values, not as TBytes values (you are thinking of the lower-layer Send() and Receive() methods of TIdLogBase).Denationalize

© 2022 - 2024 — McMap. All rights reserved.