【问题标题】:delphi 2010 - tidhttp post德尔福 2010 - tidhttp 帖子
【发布时间】:2011-05-11 19:05:00
【问题描述】:

我想将数据发布到以下网址:

http://mehratin.heroku.com/personals/new

我写了以下代码但有问题:

procedure TForm1.Button3Click(Sender: TObject);
var
  aStream: TMemoryStream;
  Params: TStringList;
begin
  aStream := TMemoryStream.Create;
  Params := TStringList.Create;
  try
    with IdHTTP1 do
    begin
      Params.Add('fname=123');
      Params.Add('lname=123');
      Request.ContentType := 'application/x-www-form-urlencoded';
      try
        Response.KeepAlive := False;
        Post('http://localhost:3000/personals/new', Params);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;

如何在 delphi 2010 中通过 TIDHtttp.post 方法发布数据?

【问题讨论】:

  • 您可以尝试删除 Request.ContentType。另外 - 确保您尝试访问的端口已打开,并且未被其他进程使用。建议改为在您的主机上尝试此操作。 ;)
  • TStrings 版本的 Post() 默认根据 'application/x-www-form-urlencoded' 内容类型对输入数据进行编码(Indy 10.5.8)
  • 错误是如何表现出来的,是否有 HTTP 错误代码,或者服务器端的数据不正确?服务器是否期望 application/x-www-form-urlencoded (或其他类似 multipart/form-data 的东西)?

标签: delphi post delphi-2010 idhttp


【解决方案1】:

首先,您需要阅读 http 响应代码(将其包含在您的问题中会很有用)。

如果没有,我之前使用过 Indy http 对象,如下所示。我在我的 URL 中包含了参数。要进行故障排除,请尝试使用 http.Get 运行它以确保端口已打开,并且您可以实际连接到服务器。这是我的完整示例:

// parameters
params := format('export=1&format=%s&file=%s', [_exportType, destination]);

// First setup the http object
procedure TCrystalReportFrame.SetupHttpObject();
begin
    try
      IDHTTP1.HandleRedirects := TRUE;
      IDHTTP1.AllowCookies := true;
      IDHTTP1.Request.CacheControl := 'no-cache';
      IdHTTP1.ReadTimeout := 60000;
       _basePath:= GetBaseUrl;
    except
      on E: Exception do
        begin
          Global.LogError(E, 'SetupHttpObject');
        end;
    end;
end;

// Then have an onwork event
procedure TCrystalReportFrame.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
  Http: TIdHTTP;
  ContentLength: Int64;
  Percent: Integer;
begin
  Http := TIdHTTP(ASender);
  ContentLength := Http.Response.ContentLength;

end;

// The actual process
procedure TCrystalReportFrame.ProcessHttpRequest(const parameters: string);
var
  url : string;
begin
  try
      try
      SetupHttpObject;
       IdHTTP1.OnWork:= HttpWork;
       url := format('%s&%s', [_basePath, parameters]);
        url := IdHTTP1.Post(url);
      except
        on E: Exception do
          begin
            Global.LogError(E, 'ProcessHttpRequest');
          end;
      end;
    finally
      try
        IdHTTP1.Disconnect;
      except
        begin
        end;
      end;
    end;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多