开发者

delphi 2010 - tidhttp 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: TM开发者_如何学GoemoryStream;
  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?


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜