Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace raw request and response content of TIdHttp

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.

like image 715
stanleyxu2005 Avatar asked Dec 23 '12 14:12

stanleyxu2005


1 Answers

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

like image 121
bummi Avatar answered Oct 01 '22 23:10

bummi