【问题标题】:Synapse HTTPServ Delphi 7 IssueSynapse HTTPServ Delphi 7 问题
【发布时间】:2014-08-11 11:18:55
【问题描述】:

我正在尝试使用 Delphi 7 中现成的 Synapse HTTPServ 示例执行以下操作。

1) Generate a simple HTML page with upload form field
2) When the user fills in the field and clicks the button, the name of the
       file is sent back to HTTPServ.
3) "get" the file and save locally
4) inspect the file
5) send back response with what was found

听起来很简单……在纸上。

在 TTCPHttpThrd.Execute 过程的 http.pas 文件中,显示如下代码。

//read request headers
If protocol <> '' then
Begin
  If pos('HTTP/', protocol) <> 1 Then Exit;
  If pos('HTTP/1.1', protocol) <> 1 Then close := true;
  Repeat
    s := sock.RecvString(Timeout);
    If sock.lasterror <> 0 Then Exit;
    If s <> '' Then Headers.add(s);
    If Pos('CONTENT-LENGTH:', Uppercase(s)) = 1 Then Size := StrToIntDef(SeparateRight(s, ' '), -1);
      // size ALWAYS returns 0 - CONTENT-LENGTH NOT IN HEADER!
    If Pos('CONNECTION: CLOSE', Uppercase(s)) = 1 Then close := true;
      // Present in header
  Until s = '';
End;

// since size ALWAYS = 0 this never gets executed to "get" the file..
InputData.Clear;
If size >= 0 Then
Begin
  InputData.SetSize(Size);
  x := Sock.RecvBufferEx(InputData.Memory, Size, Timeout);
  InputData.SetSize(x);
  If sock.lasterror <> 0 Then Exit;
End;
OutputData.Clear;
ResultCode := ProcessHttpRequest(method, uri);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ResultCode 获取以下函数的结果,根据作者 cmets 的说法,InputData 应该保存上传的文件。

它没有 - 显然是因为 size ALWAYS = 0。

function TTCPHttpThrd.ProcessHttpRequest(Request, URI: string): integer;
var
  l: TStringlist;
begin
 // sample of precessing HTTP request:
 // InputData is uploaded document, headers is stringlist with request headers.
 // Request is type of request and URI is URI of request
 // OutputData is document with reply, headers is stringlist with reply headers.
 // Result is result code

标题从不包含 CONTENT-LENGTH 字段。

所使用的表格非常简单:

<form name="getfile" action="http://127.0.0.1:80" method="get">
  <input type="file" name="uploadedfile" />
  <input type="submit" value="Submit" />
</form>

你有什么想法吗?

【问题讨论】:

    标签: delphi http synapse


    【解决方案1】:

    您的 HTML 网络表单不能使用 method="get" 上传文件,它必须使用 method="post" 代替。 HTTP GET 请求不携带任何正文数据,但 POST 携带。

    【讨论】:

    • 谢谢雷米。奇怪的是,这些年来这个例子是如何以同样的错误四处流传的。我有不同年份的多个版本,它们都使用 get。
    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2011-05-02
    相关资源
    最近更新 更多