How to send HTTP data and get response with Synapse (Delphi)
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.
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;
精彩评论