您现在的位置:首页 >> 网络通讯 >> Indy >> 内容

Delphi中用HttpCli异步POST数据

时间:2011/9/3 15:47:06 点击:

  核心提示:procedure THttpPostForm.PostButtonClick(Sender: TObject);var Data : String;begin Data := 'FirstName=...

procedure THttpPostForm.PostButtonClick(Sender: TObject);
var
    Data : String;
begin
    Data := 'FirstName=' + UrlEncode(Trim('test')) + '&' +
            'LastName='  + UrlEncode(Trim('test123'))  + '&' +
            'Submit=';
    HttpCli1.SendStream := TMemoryStream.Create;
    HttpCli1.SendStream.Write(Data[1], Length(Data));
    HttpCli1.SendStream.Seek(0, 0);
    HttpCli1.RcvdStream := TMemoryStream.Create;
    HttpCli1.URL := Trim('URL');
    HttpCli1.PostAsync;
end;

在RequestDone事件中接收和释放对象

procedure THttpPostForm.HttpCli1RequestDone(
    Sender  : TObject;
    RqType  : THttpRequest;
    ErrCode : Word);
var
    Data : String;
begin
    HttpCli1.SendStream.Free;
    HttpCli1.SendStream := nil;

    if ErrCode <> 0 then //有异常

    begin
        HttpCli1.RcvdStream.Free;
        HttpCli1.RcvdStream := nil;
        Exit;
    end;
    if HttpCli1.StatusCode <> 200 then //返回错误

    begin
        HttpCli1.RcvdStream.Free;
        HttpCli1.RcvdStream := nil;
        Exit;
    end;
    HttpCli1.RcvdStream.Seek(0, 0);
    SetLength(Data, HttpCli1.RcvdStream.Size);
    HttpCli1.RcvdStream.Read(Data[1], Length(Data));
end;

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章 技术支持:深圳市麟瑞科技有限公司(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 粤ICP备10103342号-1