Http Post with indy

后端 未结 3 799
耶瑟儿~
耶瑟儿~ 2020-12-03 17:55

I have a simple php script on my web server which I need to upload a file using HTTP POST, which I am doing in Delphi.

Here is my code with Indy but aparantely it wo

相关标签:
3条回答
  • 2020-12-03 18:22

    You lost 2 characters '--'. It is better to do so:

    http.Request.ContentType:='multipart/form-data;boundary='+myBoundery;
    fname := CRLF + '--' + myBoundery + CRLF + 'Content-Disposition: form-data; name=\"uploadedfile\";filename=\"test.png"' + CRLF;
    
    0 讨论(0)
  • 2020-12-03 18:27

    Calling a PHP from Indy can fail because of the User-Agent, then you get 403 error.

    Try this way, it fixed it for me:

    var Answer: string;
    begin
      GetHTML:= TIdHTTP.create(Nil);
      try
        GetHTML.Request.UserAgent:= 'Mozilla/3.0';
        Answer:= GetHTML.Get('http://www.testserver.com/test.php?id=1');
      finally
        GetHTML.Free;
      end;
    end;
    
    0 讨论(0)
  • 2020-12-03 18:30

    Indy has TIdMultipartFormDataStream for this purpose:

    procedure TForm1.SendPostData;
    var
      Stream: TStringStream;
      Params: TIdMultipartFormDataStream;
    begin
      Stream := TStringStream.Create('');
      try
       Params := TIdMultipartFormDataStream.Create;
       try
        Params.AddFile('File1', 'C:\test.txt','application/octet-stream');
        try
         HTTP.Post('http://posttestserver.com/post.php', Params, Stream);
        except
         on E: Exception do
           ShowMessage('Error encountered during POST: ' + E.Message);
        end;
        ShowMessage(Stream.DataString);
       finally
        Params.Free;
       end;
      finally
       Stream.Free;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题