【转】 delphi IDHTTP 登录网站教程代码 2010-08-12 14:51 转载自 s_jqzhang 最终编辑 zxp922 下面代码是 delphi2010 的,主要区别也就在于 stringstream 的 初使化, Response := TStringStream.Create(”, TEncoding.UTF8); 改成 Response := TStringStream.Create(‘ ‘); unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls; type TForm1 = class(TForm) mmo1: TMemo; btn1: TButton; IdHTTP1: TIdHTTP; Label1: TLabel; Edit1: TEdit; Label2: TLabel; Edit2: TEdit; procedure btn1Click(Sender: TObject); private
{ Private declarations }
public public
{ Public declarations }
end; end var Form1: TForm1; implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject); var
code: string string; done: Boolean Boolean; postList: TStrings; Response: TStringStream; begin mmo1.clear;
// 开始登录代码
Response := TStringStream.Create('', TEncoding.UTF8); postList := TStringList.Create; try IdHTTP1.HandleRedirects := True True; postList.add('cktime=31536000'); postList.add('forward='); postList.add('hideid=0'); postList.add('jumpurl=bbs.hualongxiang/index.php'); postList.add('lgt=0'); postList.add('pwpwd='+edit2.Text ); postList.add('pwuser='+edit1.Text ); postList.add('step=2'); postList.add('submit=?'); IdHTTP1.Post('
bbs.hualongxiang/login.php?', postList, Response); done := True True; except done := false false; end; end if (done) and (Pos('200 OK', IdHTTP1.ResponseText) <> 0) then begin code := IdHTTP1.Get('bbs.hualongxiang/index.php'); mmo1.lines.add(code); end else begin mmo1.lines.add('logon error,error code:' + IdHTTP1.Response.RawHeaders.Text); end; end Response.Free; postList.Free; end; end