uses JSON, JSON.Types, JSON.Writers, JSON.Builders, Wininet;
procedure post;
var
inetSession, inetConnection, inetRequest: HINTERNET;
js, jsElement: TJSONObject;
JSarray: TJSONArray;
Parameter:
String
begin
if inetSession =
nil then inetSession := InternetOpen(PChar('
Delphi App'), INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil, 0);
if inetSession =
nil then RaiseLastOSError;
if inetConnection =
nil then inetConnection := InternetConnect(inetSession, PChar('
###URL z.B.: www.demo.de ###'), INTERNET_DEFAULT_HTTPS_PORT,
nil,
nil, INTERNET_SERVICE_HTTP, 0, 1);
if inetConnection =
nil then RaiseLastOSError;
inetRequest := HttpOpenRequest(inetConnection, PChar('
POST'),
nil,
nil,
nil,
nil, INTERNET_FLAG_SECURE, 1);
if inetRequest =
nil then RaiseLastOSError;
//JSON aufbauen
js := TJSONObject.Create;
JSarray := TJSONArray.Create;
jsElement := TJSONObject.Create;
try
js.AddPair(TJSONPair.Create('
Propertyname', '
ABC'));
jsElement.AddPair(TJSONPair.Create('
arrayElement', TJSONNumber.Create(10)));
JSarray.AddElement(jsElement);
js.AddPair(TJSONPair.Create('
array', JSarray));
Parameter := js.ToJSON;
finally
jsElement.free;
JSarray.free;
js.free;
end;
//JSON posten
if not HttpAddRequestHeaders(inetRequest, PChar(Header), Length(Header), HTTP_ADDREQ_FLAG_ADD)
then RaiseLastOSError;
if not HTTPSendRequest(inetRequest,
nil, 0, PAnsiChar(AnsiString(Utf8Encode(Parameter))), length(Utf8Encode(Parameter)))
then RaiseLastOSError;
end;