How to send HTTP data and get response with Synapse (Delphi)
Asked Answered
W

1

5

I think the title was clear enough. I want to know how to send HTTP POST request with parameters/arguments and receive HTML response back - using Synapse library for Delphi.

Weigle answered 9/6, 2011 at 10:29 Comment(0)
F
8

Try to use HttpPostURL function.

function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;

URL - target URL
URLData - URL parameters; must be encoded e.g. using EncodeURLElement function
Data - target stream, where the response will be stored

The following example uses testing POST server where send two POST parameters. Note using of EncodeURLElement function for encoding parameter data. If the POST succeed the server response is saved into the file.

uses HTTPSend, Synacode;

procedure TForm1.Button1Click(Sender: TObject);
var URL: string;
    Params: string;
    Response: TMemoryStream;

begin
  Response := TMemoryStream.Create;

  try
    URL := 'http://posttestserver.com/post.php?dump&html';

    Params := 'parameter1=' + EncodeURLElement('data1') + '&' +
              'parameter2=' + EncodeURLElement('data2');

    if HttpPostURL(URL, Params, Response) then
      Response.SaveToFile('c:\response.txt');

  finally
    Response.Free;
  end;
end;
Feria answered 9/6, 2011 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.