分享

使用IDHTTP登录网站

 anggi_luo 2010-10-30

使用IDHTTP登录网站

解答2ccc.com上一个朋友的问题
使用idhttp来登录一个网站,正好手头装了delphi2010就写了代码,后来用delphi7试了一下不得,搞不懂是不是delphi7自带的indy的组件是不是太老。升到2010就不想再回去了,哈哈,要一直用delphi7可以升级一下indy组件,我想应当没问题的
关于post的参数从哪里找,使用httpwatch,可以监控post数据,直接按监控数据写就得了,如下图:

下面代码是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 declarations }
end;
 
var
Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.btn1Click(Sender: TObject);
var
code: string;
done: Boolean;
postList: TStrings;
Response: TStringStream;
begin
mmo1.clear;
 
// 开始登录代码
Response := TStringStream.Create('', TEncoding.UTF8);
postList := TStringList.Create;
try
IdHTTP1.HandleRedirects := True;
 
postList.add('cktime=31536000');
postList.add('forward=');
postList.add('hideid=0');
postList.add('jumpurl=http://bbs./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('http://bbs./login.php?', postList, Response);
done := True;
except
done := false;
end;
 
if (done) and (Pos('200 OK', IdHTTP1.ResponseText) <> 0) then
begin
code := IdHTTP1.Get('http://bbs./index.php');
mmo1.lines.add(code);
end
else
begin
mmo1.lines.add('logon error,error code:' + IdHTTP1.Response.RawHeaders.Text);
end;
Response.Free;
postList.Free;
 
end;
 
 
 
end.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多