English 中文(简体)
Http 带内装的 Http 邮报
原标题:Http Post with indy

我的网络服务器上有一个简单的php脚本, 我需要用 HTTP POST 上传一个文件, 我在德尔斐正在这样做。

我和Indy的代码在这里,但有保证它不会起作用,我无法弄清楚我做错了什么。我怎么能看到我发送到服务器上的东西有这样一个工具呢?

procedure TForm1.btn1Click(Sender: TObject);
var
  fname : string;
  MS,dump : TMemoryStream;
  http  : TIdHTTP;

const
  CRLF = #13#10;
begin
  if PromptForFileName(fname,  ,  ,  ,  ,false) then
  begin
    MS := TMemoryStream.Create();
    MS.LoadFromFile(fname);
    dump := TMemoryStream.Create();
    http := TIdHTTP.Create();
    http.Request.ContentType:= multipart/form-data;boundary =-----------------------------7cf87224d2020a ;
    fname := CRLF +  -----------------------------7cf87224d2020a  + CRLF +  Content-Disposition: form-data; name="uploadedfile";filename="test.png"  + CRLF;
    dump.Write(fname[1],Length(fname));
    dump.Write(MS.Memory^,MS.Size);
    fname := CRLF +  -----------------------------7cf87224d2020a--  + CRLF;
    dump.Write(fname[1],Length(fname));
    ShowMessage(IntToStr(dump.Size));
    MS.Clear;
    try
    http.Request.Method :=  POST ;
    http.Post( http://posttestserver.com/post.php ,dump,MS);
    ShowMessage(PAnsiChar(MS.Memory));
    ShowMessage(IntToStr(http.ResponseCode));
    except
    ShowMessage( Could not bind socket );
    end;
  end;
end;
最佳回答

Indy为此目的有TIdMultipart FormDataStream :

procedure TForm1.SendPostData;
var
  Stream: TStringStream;
  Params: TIdMultipartFormDataStream;
begin
  Stream := TStringStream.Create(  );
  try
   Params := TIdMultipartFormDataStream.Create;
   try
    Params.AddFile( File1 ,  C:	est.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;
问题回答

从 Indy 调用 PHP 时会因用户代理失败, 然后会发生403个错误 。

试试这个,它帮我修好了它:

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;

你失去了两个字符--。最好这样做:

http.Request.ContentType:= multipart/form-data;boundary= +myBoundery;
fname := CRLF +  --  + myBoundery + CRLF +  Content-Disposition: form-data; name="uploadedfile";filename="test.png"  + CRLF;




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签