学习indy组件之一idhttp的使用方法
【转】学习indy组件之一idhttp的使用方法
预备知识 IdHTTP组件是Indy组件的一部分,主要用于实现读取HTTP服务器的资源,可以实现浏览器的网络功能。
IdHTTP是从TIdCustomHTTP继承来的,基本上也就是换了一个比较简单的名字给类和属性而已,它本身没有自己的函数和过程,全是从TIdCustomHTTP继承来的。基本上用得到的两个方法是Get(两个重载类型)和Post(四个重载类型)。
idhttp的post方法:
function Post(AURL: string; const ASource: TStrings): string; overload;
function Post(AURL: string; const ASource: TStream): string; overload;
function Post(AURL: string; const ASource: TIdMultiPartFormDataStream): string; overload;
procedure Post(AURL: string; const ASource: TStrings; const AResponseContent: TStream); overload;
procedure Post(AURL: string; const ASource: TStream; const AResponseContent: TStream); overload;
procedure Post(AURL: string; const ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload;
2、异常响应为开发者提供了一个按自己的需要进行异常处理的机制。主要异常处理有两种: try except end
try finally
try …except …end形成了一个异常响应保护块。与finally不同的是:正常情况下except 后面的语句并不被执行,而当异常发生时程序自动跳到except,进入异常响应处理模块。当异常被响应后异常类自动清除。相关文章http://www.fosu.edu.cn/netschool/delphi/029.htm
主要代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Memo1: TMemo;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Button1: TButton;
Button2: TButton;
Edit4: TEdit;
Label6: TLabel;
IdHTTP1: TIdHTTP;
Memo2: TMemo;
Label7: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
mytemp:tStringList; //变量定义
sex:string;
begin
if RadioButton1.Checked=true then begin //判断性别
sex:=RadioButton1.Caption;
end;
if RadioButton2.Checked=true then begin
sex:=RadioButton2.Caption;
end;
//以下值都来自http://www.gxxezz.com/liuyan/index.asp的页面分析,分析出其中的必要的提交元素,不要忘记hidden类型的
mytemp:=tStringList.Create;
mytemp.Add('NAME='+edit1.Text);
mytemp.Add('sex='+sex);
mytemp.Add('email='+edit2.Text);
mytemp.Add('WEB='+edit3.Text);
mytemp.Add('MESSAGE='+Memo1.Lines.Text);
mytemp.Add('Send=1'); //这个是个隐藏属性,一定得加啊
try
IdHTTP1.Post('http://www.gxxezz.com/liuyan/index.asp',mytemp); //idhttp提交post请求,如果攻击其他的留言本得更换提交地址
except
Memo2.Lines.Add('估计是成功了'); //发送完毕记录
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if timer1.Enabled=false then
begin
timer1.Interval:=strtoint(Edit4.Text); //设定攻击间隔
timer1.Enabled:=true; //激活定时器
Button2.Caption:='停止攻击'; //改变按钮文字
end
else begin
timer1.Enabled:=false; //停止定时器
Button2.Caption:='自动攻击'; //改变按钮文字
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
application.ProcessMessages; //防止界面死掉
Button1Click(self); //按照设定的时间间隔自动点击发送按钮
end;
end.
----------------------------------------------------------------------------------------------------
var
Fpost : Tstrings;
sReturn : String;
res : TstringStream;
begin
// 如果你的提交内容需要,encode
idhttp1.HTTPOptions :=[hoForceEncodeParams];
// 如果你的提交内容不需要encode
// idhttp1.HTTPOptions :=[];
// 如果你的提交内容成公后需要redirct,通常这个设置
idhttp1.HandleRedirects :=true;
Fpost :=TstringList.create;
Fpost.text :='username=yourname&password=yourpwd';
res := TstringStream.Create('');
try
idhttp1.post(postURL,Fpost,res);
finally
sReturn :=Res.DataString;
res.Free;
end;
Fpost.free;
//你可以判断 sReturn 中标志判断了
end;
---------------------------------------------------------------------------------------------
今晚花了很长时间,到处搜索~~弄了很多东西~~总结一下先~!
从webbrowsers说起:
wb1.Navigate("http://hi.baidu.com");跳转到这个页面
wb1.OleObject.document.getElementByID('username').innerText:='fatkun';//如果知道ID
wb1.OleObject.document.getElementsByTagName('input').item(0).click;//如果知道第几个按钮
wb1.OleObject.document.all.item('userlogin', 0).click;//如果知道NAME
用webbbrowsers取得cookies
首先,要加入 uses MSHTML;
var Doc:IHTMLDocument2;
begin
Doc:=wb1.Document as IHTMLDocument2;
Doc.cookie;//这个就是cookies了。。
end;
更详细可以参考了万一老师的博客文章:WebBrowser 操作记要
IDHTTP:
//这里POST的具体数据,你可以使用WinSock Expert抓包来获取,然后每一项加在TSTRINGS里面发出去就可以了。
procedure TForm1.btn5Click(Sender: TObject);//测试登陆DISCUZ6.0,6.1都成功!
var
ts:TStrings;
str:string;
begin
ts:=TStringList.Create;
ts.Add('formhash=8');
ts.Add('cookietime=2592000');
ts.Add('loginfield=username');
ts.Add('username=你的用户名');
ts.Add('password=你的密码');
ts.Add('userlogin');
str:=IdHTTP1.Post('http://bbs.focusmobile.cn/logging.php?action=login&loginsubmit=true',ts);
mmo1.Text:=str;
end;
在IdCookieManager1中取得cookies,当然要设置
IdHTTP1.CookieManager:=IdCookieManager1;
for i := 0 to IdCookieManager1.CookieCollection.Count - 1 do
cookieStr:=cookieStr+(IdCookieManager1.CookieCollection.Items[i].CookieText);
取得cookies有什么用呢?当然有用啦.
拿到cookies后,我们可以把cookies赋值给另一个idhttp
idHttp2.Request.CustomHeaders.Text := 'Cookie: '+cookieStr;
浙公网安备 33010602011771号