unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls, System.Net.URLClient,
System.Net.HttpClient, System.Net.HttpClientComponent;
type
TForm1 =
class(TForm)
Button1: TButton;
NetHTTPClient1: TNetHTTPClient;
procedure Button1Click(Sender: TObject);
private
sErr:
string;
slParams: TStringList;
slHeaders: TStringList;
Function doGetData(sBody, BodyMode,
url:
String;
var sRP:
String): Boolean;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
sBody:
string;
url:
string;
sRP:
string;
bodyMode:
string;
begin
slParams:= TStringList.Create;
slHeaders:= TStringList.Create;
try
sBody := '
{"Hasta":"Lavista Baby "}';
bodyMode := '
raw';
url:= '
deine Ziel URL';
if doGetData(sBody,bodyMode,
url,sRP)
then
showmessage(sRP);
finally
slParams.Free;
slHeaders.Free;
end;
end;
function TForm1.doGetData(sBody, BodyMode,
url:
String;
var sRP:
String): Boolean;
var
uri: TUri;
srRQBody: TStringStream;
RP: IHTTPResponse;
sRQContent:
String;
sRPContent:
String;
srRPBody: TStringStream;
I: Integer;
StatusCode: Integer;
StatusText:
String;
ResponseText:
String;
begin
Result := false;
srRQBody:= TStringStream.Create;
srRPBody:= TStringStream.Create;
try
//-- URL
try
uri := TUri.Create(
URL);
except on E:
Exception do
sErr:= E.
Message;
end;
//-- Params
for I := 0
to slParams.Count-1
do
uri.AddParameter(slParams.Names[i], slParams.ValueFromIndex[i]);
//-- Auth
//SetAuth(RQRec);
//-- Headers
for I := 0
to slHeaders.Count-1
do
NetHTTPClient1.CustomHeaders[slHeaders.Names[i]] := slHeaders.ValueFromIndex[i];
//-- Body
sRQContent := sBody;
if BodyMode='
raw'
then
begin
srRQBody.WriteString(sRQContent);
srRQBody.Position := 0;
end else
if BodyMode='
x-www-form-urlencoded'
then
begin
sRQContent := '
data'+'
='+sRQContent;
srRQBody.WriteString(sRQContent);
srRQBody.Position := 0;
end;
RP := NetHTTPClient1.Post(
uri.ToString, srRQBody, srRPBody);
sRPContent := srRPBody.DataString;
StatusCode := RP.StatusCode;
StatusText := RP.StatusText;
ResponseText := sRPContent;
if StatusCode=200
then
Result := true;
finally
srRQBody.Free;
srRPBody.Free;
end;
end;
end.