有些网站的部分信息只允许已登录的用户才能查看,通过IdHttp可以模拟这个登录过程。下面我们以登录学炒股网(http://xuestock.cn)网站为例,来说明这个过程。
首先,需要在学炒股网(http://xuestock.cn)网站注册一个用户,我注册了joke这个用户,密码是123456;接着我们需要跟踪一下登录的过程用到了哪些参数。使用MiniSniffer软件可以很容易的捕获这个登录过程。MiniSniffer的使用,我在已经介绍过了。MiniSniffer捕获的信息是
POST /member/login.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*
Referer: http://www.xuestock.cn/
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
Host: www.xuestock.cn
Content-Length: 69
Connection: Keep-Alive
Cache-Control: no-cache
username=joke&password=123456&cookietime=0&dosubmit=%B5%C7+%C2%BC
根据这些捕获的信息,我们能够了解很多内容:
登录的地址是http://www.xuestock.cn/member/login.php
登录的参数格式是username=joke&password=123456&cookietime=0&dosubmit=%B5%C7+%C2%BC
内容类型是application/x-www-form-urlencoded
有了这些信息,那么我们开始模拟登陆吧:
var
stream: TStringStream;
begin
IdHttp1.HandleRedirects := True;
IdHttp1.Request.ContentType := 'application/x-www-form-urlencoded';
stream := TStringStream.Create('username=joke&password=1234561234&cookietime=0&dosubmit=%B5%C7+%C2%BC');
try
memo1.Lines.Add(IdHttp1.Post('http://www.xuestock.cn/member/login.php',stream));
finally
stream.Free;
end;
评论