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.

stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
  • Thanks for the hints of @bummi. After seeing the log, I saw TIdHttp sends incorrect protocol version, unless I set Client.HTTPOptions := [hoKeepOrigProtocol]. – stanleyxu2005 Dec 23 '12 at 17:00

1 Answers1

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.

bummi
  • 27,123
  • 14
  • 62
  • 101
  • 4
    `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`). – Remy Lebeau Dec 23 '12 at 19:38