如何使用Synapse发送文件和其他POST数据

时间:2012-07-13 13:31:08

标签: delphi file http post synapse

使用Delphi: 2007。

您好,

我有一个简单的网页,有两个文本输入和一个文件输入。现在,对于要发送的表单,必须填写文本输入和文件输入。使用Synapse,我知道如何上传文件( HttpPostFile )以及如何发布数据( HttpMethod )。但是,我不知道如何做到这两点。

在查看Synapse的源代码之后,我想我必须用边界或类似的东西“格式化”我的数据。我想我的输入文件应该有一个边界,我的文本输入应该有另一个边界。我发现了一篇关于这个主题的文章,但它是关于发送电子邮件附件的。我尝试重现他们用Synapse所说的话,没有结果。

HttpPostFile的代码

function HttpPostFile(const URL, FieldName, FileName: string;
  const Data: TStream; const ResultData: TStrings): Boolean;
var
  HTTP: THTTPSend;
  Bound, s: string;
begin
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
  HTTP := THTTPSend.Create;
  try
    s := '--' + Bound + CRLF;
    s := s + 'content-disposition: form-data; name="' + FieldName + '";';
    s := s + ' filename="' + FileName +'"' + CRLF;
    s := s + 'Content-Type: Application/octet-string' + CRLF + CRLF;
    WriteStrToStream(HTTP.Document, s);
    HTTP.Document.CopyFrom(Data, 0);
    s := CRLF + '--' + Bound + '--' + CRLF;
    WriteStrToStream(HTTP.Document, s);
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
    Result := HTTP.HTTPMethod('POST', URL);
    if Result then
      ResultData.LoadFromStream(HTTP.Document);
  finally
    HTTP.Free;
  end;
end;

谢谢。

3 个答案:

答案 0 :(得分:4)

你的代码很接近。您只发送文件字段,但不发送文本字段。要做到这三点,请尝试这样做:

function HttpPostFile(const URL, InputText1FieldName, InputText1, InputText2FieldName, InputText2, InputFileFieldName, InputFileName: string; InputFileData: TStream; ResultData: TStrings): Boolean; 
var 
  HTTP: THTTPSend; 
  Bound: string; 
begin 
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary'; 
  HTTP := THTTPSend.Create; 
  try 
    WriteStrToStream(HTTP.Document,
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputText1FieldName, '"') + CRLF +
      'Content-Type: text/plain' + CRLF +
      CRLF); 
    WriteStrToStream(HTTP.Document, InputText1); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputText2FieldName, '"') + CRLF +
      'Content-Type: text/plain' + CRLF +
      CRLF); 
    WriteStrToStream(HTTP.Document, InputText2); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + CRLF +
      'Content-Disposition: form-data; name=' + AnsiQuotedStr(InputFileFieldName, '"') + ';' + CRLF + 
      #9'filename=' + AnsiQuotedStr(InputFileName, '"') + CRLF +
      'Content-Type: application/octet-string' + CRLF +
      CRLF); 
    HTTP.Document.CopyFrom(InputFileData, 0); 
    WriteStrToStream(HTTP.Document,
      CRLF +
      '--' + Bound + '--' + CRLF); 
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound; 
    Result := HTTP.HTTPMethod('POST', URL); 
    if Result then 
      ResultData.LoadFromStream(HTTP.Document); 
  finally 
    HTTP.Free; 
  end; 
end; 

如果您切换到Indy,您可以使用其TIdMultipartFormDataStream类:

function HttpPostFile(const URL, InputText1FieldName, InputText1, InputText2FieldName, InputText2, InputFileFieldName, InputFileName: string; InputFileData: TStream; ResultData: TStrings): Boolean; 
var 
  HTTP: TIdHTTP; 
  Input: TIdMultipartFormDataStream;
  Output: TMemoryStream;
begin 
  Result := False;
  try
    Output := TMemoryStream.Create;
    try
      HTTP := TIdHTTP.Create; 
      try 
        Input := TIdMultipartFormDataStream.Create;
        try
          Input.AddFormField(InputText1FieldName, InputText1);
          Input.AddFormField(InputText2FieldName, InputText2);
          Input.AddFormField(InputFileFieldName, 'application/octet-stream', '', InputFileData, InputFileName);
          HTTP.Post(URL, Input, Output);
        finally
          Input.Free;
        end;
      finally
        HTTP.Free;
      end;
      Output.Position := 0;
      ResultData.LoadFromStream(Output);
      Result := True; 
    finally
      Output.Free;
    end;
  except
  end; 
end; 

答案 1 :(得分:1)

我也在项目中使用了synapse。为了使我的工作简单快捷,使用Synapse,我编写了THTTPSendEx类,它提供了快速的使用速度和最少的代码以及更多的功能。 目前它是测试版。

像Indy这样的观点。

创建THTTPSendEx类。 从它原型创建方法OnBeginWork,OnWork,OnWorkEnd(参见pas文件),并将其分配给创建的类。这就是你所需要的,只需要调用该类的GET,POST函数。

我还实现了multipart-fomdata,以此格式快速发布文件为TMultipartFormDataStream类。 有了它,您可以轻松编写文件和字段。

使用示例:

var
 HTTP:THTTPSendEx;
 Data:TMultipartFormDataStream;
 sHTML:string; //Recived HTML code from web
begin
 HTTP:=THTTPSEndEx.Create;
 Data:=TMultipartFormDataStream.Create;
 try
  Data.AddFile('myFile','Path to the local file(No UNC paths)');
  Data.DataEnd;
  if HTTP.Post('URL HERE',Data,sHTML) then
  begin
  //Connection established
  //Check HTTP response
  if HTTP.IsSuccessfull then  //HTTP returns "200 OK" code.
  begin
    ShowMessage('File successfully posted to the server.');
  end;
  end else
  begin
   ShowMessage('Can not establish a connection to the server...'+#13+'Network is not avaliable or server socket does not exist.');
  end;
 finally
   FreeAndNil(HTTP);
   FreeAndNil(Data);
 end;
end;

You can see it at my web-site

如果您对此有任何想法,请将其作为对项目页面的评论。

对不起英文错误。

答案 2 :(得分:0)

好读:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

问题解决了。我保留公开的问题,以防有一天有人有同样的问题。