delphi 2010 - tidhttp 帖子

发布于 2024-11-06 18:32:07 字数 824 浏览 0 评论 0原文

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

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

i want to post data to the following url:

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

i write the following code but has problem:

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;

how can i post data by TIDHtttp.post method in delphi 2010?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-11-13 18:32:07

首先,您需要阅读 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;

First things first, you'd need to read the http response code (it would have been useful to include that in your question).

In the absence of that I've used the Indy http object before as shown below. I included the parameters in my URL though. To troubleshoot, try running this with an http.Get to ensure the port is open, and you can actually connect to the server. Here's my example for completenes:

// 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文