Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Per indy mehre Datein als Multipart über https versenden. (https://www.delphipraxis.net/195243-per-indy-mehre-datein-als-multipart-ueber-https-versenden.html)

Kostas 14. Feb 2018 10:53

Per indy mehre Datein als Multipart über https versenden.
 
Hallo Zusammen,

ich verwende die folgende Methode um eine Datei per HTTPS zu versenden. Die Datei wird als HTTP-Payload in Binärformat gesendet. Jetzt benötige ich eine Möglichkeit mehrere Bilder an einen fremden Server zu versenden der die Bilder nicht als HTTP-Payload empfangen kann sondern als Multipart.

Der Server ist also nicht von mir. Ich muss Multipart verwenden!

Hat mir jemand bitte einen Hinwies wie das geht?


Delphi-Quellcode:
function TForm3.SendFileViaHTTPS(aFile:String):String;
var aResponceStream: TMemoryStream;
    aFileName:string;
begin
  aFileName := ExtractFileName(aFile);

  IdHTTP1.Request.BasicAuthentication := True;
  IdHTTP1.Request.Username := 'Username';
  IdHTTP1.Request.Password := 'Password';

  aResponceStream := TMemoryStream.Create();
  aResponceStream.Clear;
  aResponceStream.Seek(0,soFromBeginning);
  try
    try
      IdHTTP1.Post(Format('https://services.irgendwas.de/upload-api/upload/%s',[aFileName]), aFile, aResponceStream);
      aResponceStream.Seek(0,soFromBeginning);
      Memo1.Lines.LoadFromStream(aResponceStream);
      Memo1.Lines.Add(IdHTTP1.ResponseCode.ToString);
    except
      on E: EIdHTTPProtocolException do
      begin
        aResponceStream.Seek(0,soFromBeginning);
        Memo1.Lines.LoadFromStream(aResponceStream);
        Memo1.Lines.Add(IdHTTP1.ResponseCode.ToString);
      end;
    end;

  finally
    aResponceStream.Free;
  end;

end;
Gruß Kostas

HolgerX 14. Feb 2018 11:47

AW: Per indy mehre Datein als Multipart über https versenden.
 
Hmm..

So nach diesem Muster?

Delphi-Quellcode:
var
  IdHTTP: TIdHTTP;
  PostData: TIdMultipartFormDataStream;
begin
  AUrl := EncodeURLA(AUrl);

  IdHTTP := TIdHTTP.Create(nil);
  try


    PostData := TIdMultipartFormDataStream.Create;
    try
      PostData.AddFile('Test_File',AFileName);
      PostData.AddFormField('Text_in_UTF8', Utf8Encode(AData),'utf-8').ContentTransfer := 'binary';
      PostData.AddFormField('Text_in_ASCII', AData, 'ISO-8859-1').ContentTransfer := 'binary';
      result := IdHTTP.Post(AURL,PostData);
    finally
      PostData.Free;
    end;
  finally
    IdHTTP.Free;
  end;
end;

Kostas 14. Feb 2018 19:45

AW: Per indy mehre Datein als Multipart über https versenden.
 
Hi HolgerX,

oh, da sieht sehr seht gut aus. Ich habe es gleich ausprobiert und bekomme immer die Fehlermeldung "Error: HTTP/1.0 415 Unsupported Media Type" Das dürfe jetzt nur noch eine Kleinigkeit sein. Das bekomme ich schon hin.

Delphi-Quellcode:
PostData.AddFile('Test_File',AFileName, 'image/jpeg');
Tausend Dank dir.
Gruß Kostas

Kostas 19. Feb 2018 13:30

AW: Per indy mehre Datein als Multipart über https versenden.
 
Hi HolgerX,

der Fremdserver benötigt das senden über PUT als Multipart.

So sieht der aktuell Umbau aus. Doch leider bekommen ich damit die Fehlermeldung "HTTP/1.1 400 Bad Request."
Der Anbieter meinte ich sende nicht als Multipart.

Sicherlich hat er recht. Kannst du erkennen was hier schief laufen könnte?



Delphi-Quellcode:
procedure TfrMain.Button2Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  PostData: TIdMultipartFormDataStream;
  PostResponse:String;

begin

  IdHTTP := TIdHTTP.Create(nil);
  try

    PostData := TIdMultipartFormDataStream.Create;
    try
      IdHTTP.ProxyParams.ProxyServer := 'api.sandbox.XY.de';
      IdHTTP.ProxyParams.ProxyPort := 8080;

      IdHTTP.Request.Accept := 'application/data.XY.api+json';
      IdHTTP.Request.ContentType := 'multipart/form-data';
      IdHTTP.Request.BasicAuthentication := true;
      IdHTTP.Request.Host := 'services.XY.de';
      IdHTTP.Request.Username := 'User';
      IdHTTP.Request.Password := 'password';

      PostData.AddFile('image2', 'F:\Bilder\A02-6176_2.jpg');

      Memo1.Lines.Add(IdHTTP.put('https://services.XY.de/api/1580/ad/358943/images',PostData));

    finally
      PostData.Free;
    end;
  finally
    IdHTTP.Free;
  end;

end;
Gruß Kostas

HolgerX 19. Feb 2018 17:44

AW: Per indy mehre Datein als Multipart über https versenden.
 
Hmm..

Schau mal hier:

https://stackoverflow.com/questions/...e-with-tidhttp

Zitat:

I found the problem. It was about the boundary that I must to set. So the final result is:

Delphi-Quellcode:

DS := TIdMultiPartFormDataStream.Create;
DS.AddFile('fileUpload2', 'C:\Users\r.rezino\Desktop\teste.po', 'application/octet-stream');
try
    FHttpComunication.Request.ContentType := 'multipart/form-data; boundary=' + DS.Boundary;
    FHttpComunication.Request.AcceptEncoding := '';
    FHttpComunication.Request.Accept := '';
    FHttpComunication.Request.Host := '';
    FHttpComunication.Request.UserAgent := '';

    FHttpComunication.Put(UrlCmd, DS, Response);
finally
    DS.Free;
    Response.Free;
end;

Wenn ich das richtig verstanden habe, wird beim POST automatisch das richtige ContentType und boundary gesetzt.
Jedoch bei PUT musst Du das selber machen..

Kostas 20. Feb 2018 10:17

AW: Per indy mehre Datein als Multipart über https versenden.
 
Hi HolgerX,

dank deiner Hilfe funktioniert es jetzt bei mir und zwar so:



Delphi-Quellcode:
procedure TfrMain.Button2Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  FPutData: TIdMultipartFormDataStream;

begin

  IdHTTP := TIdHTTP.Create(nil);
  try

    FPutData := TIdMultipartFormDataStream.Create;
    FPutData.AddFile('image1', 'F:\Bilder\A02-6176_1.jpg', 'image/jpeg');
    FPutData.AddFile('image2', 'F:\Bilder\A02-6176_2.jpg', 'image/jpeg');
    FPutData.AddFile('image3', 'F:\Bilder\A02-6176_3.jpg', 'image/jpeg');


    try
      IdHTTP.ProxyParams.ProxyServer := 'api.sandbox.XY.de';
      IdHTTP.ProxyParams.ProxyPort := 8080;

      IdHTTP.Request.Accept := 'application/data.XY.api+json';
      IdHTTP.Request.ContentType := FPutData.RequestContentType;  <<< Das ist erforderlich
      IdHTTP.Request.BasicAuthentication := true;
      IdHTTP.Request.Host := 'services.XY.de';
      IdHTTP.Request.Username := 'User';
      IdHTTP.Request.Password := 'password';


      Memo1.Lines.Add(IdHTTP.put('https://services.XY.de/api/1580/ad/358943/images',FPutData));

    finally
      FPutData.Free;
    end;
  finally
    IdHTTP.Free;
  end;

end;


Herzlichen Dank und einen schönen Tag.

Gruß Kostas


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:53 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz