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;

Last modification:August 18th, 2010 at 09:08 pm
如果觉得我的文章对你有用,请随意赞赏