Einzelnen Beitrag anzeigen

jus

Registriert seit: 22. Jan 2005
343 Beiträge
 
Delphi 2007 Professional
 
#6

AW: Kommunikation mit Windows WCF Dienst

  Alt 2. Apr 2016, 00:47
ok, nun geht es. War wieder Mal mein Fehler. Hatte im JSON einen Abschreibfehler gehabt.
@mjustin Danke nochmals für den Code!

Habe den folgenden Code geringfügig an Delphi 2007 angepaßt, damit es compiliert.

Delphi-Quellcode:
program JSONPostExample;

{$APPTYPE CONSOLE}

uses
  IdHTTP, IdGlobal, SysUtils, Classes;

var
  HTTP: TIdHTTP;
  RequestBody: TStream;
  ResponseBody: string;
  s: String;
begin
  HTTP := TIdHTTP.Create;
  try
    try
      s := '{"ftID":"TESTID","cbD":"1"}';
      writeln(s);
      RequestBody := TStringStream.Create(s);
      try
        HTTP.Request.Accept := 'application/json';
        HTTP.Request.ContentType := 'application/json'; //<------ mußte bei mir zwingend drinstehen, damit es funktioniert!!!
        HTTP.Request.ContentEncoding:= 'utf-8';

        ResponseBody := HTTP.Post('http://localhost:1201/testsample/POS/json/sign', 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;
  ReportMemoryLeaksOnShutdown := True;
end.
  Mit Zitat antworten Zitat