Einzelnen Beitrag anzeigen

mjustin

Registriert seit: 14. Apr 2008
3.004 Beiträge
 
Delphi 2009 Professional
 
#2

AW: Verbindung zu Online-Shop

  Alt 21. Apr 2018, 18:17
Ein einzelner, minimaler HTTPS POST eines JSON Objekts mit Indy ist überschaubar:

Delphi-Quellcode:
program JSONPostExample;
 
{$APPTYPE CONSOLE}
 
uses
  IdHTTP, IdGlobal, SysUtils, Classes;
 
var
  HTTP: TIdHTTP;
  RequestBody: TStream;
  ResponseBody: string;
begin
  HTTP := TIdHTTP.Create;
  try
    try
      RequestBody := TStringStream.Create('{"日本語":42}',
        TEncoding.UTF8);
      try
        HTTP.Request.Accept := 'application/json';
        HTTP.Request.ContentType := 'application/json';
        ResponseBody := HTTP.Post('https://httpbin.org/post',
          RequestBody);
        WriteLn(ResponseBody);
        WriteLn(HTTP.ResponseText);
      finally
        RequestBody.Free;
      end;
    except
      on E: EIdHTTPProtocolException do
      begin
        WriteLn(E.Message);
        WriteLn(E.ErrorMessage);
      end;
      on E: Exception do
      begin
        WriteLn(E.Message);
      end;
    end;
  finally
    HTTP.Free;
  end;
  ReadLn;
end.
Quellen:
- https://mikejustin.wordpress.com/201...-6-https-post/
- http://stackoverflow.com/a/28493431/80901
- http://stackoverflow.com/a/9254967/80901
- http://www.indyproject.org/sockets/b.../20141222.aspx

Die OpenSSL DLLS müssen dazu im Programmverzeichnis liegen.
(JSON ist neben XML ein häufig in HTTP-APIs eingesetzter Standard)
Michael Justin
  Mit Zitat antworten Zitat