delphi 2010 - tidhttp帖子

时间:2011-05-11 19:05:00

标签: delphi post delphi-2010 idhttp

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

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方法发布数据?

1 个答案:

答案 0 :(得分:0)

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

如果没有,我之前使用了Indy http对象,如下所示。我将参数包含在我的网址中。要进行故障排除,请尝试使用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;
相关问题