procedure TForm1.Button1Click(Sender: TObject);
const
DFW_LOGIN_URL = 'http://www.delphibbs.com/delphibbs/chkuser.asp';
UserName = '你的用户名';
Password = '你的密码'; //晕,刚才把密码写出来了,得改一下了
var
Params: TStrings;
HTML: String;
begin
Params:=TStringList.Create;
try
Params.Add('URL='+'/delphibbs/collections.asp');
//登录成功后跳转到的URL,这里直接转到"我收藏的问题"
Params.Add('QUERY_STRING='); //登录成功后跳转URL的参数
Params.Add('txtName='+UserName);
Params.Add('txtPass='+Password);
//Params.Add('chkSave='); //是否记住我的密码
IdHttp.HandleRedirects:=True;
HTML:=IdHttp.Post(DFW_LOGIN_URL,Params);
if Pos('<USER Name="" />',HTML)>0 then
ShowMessage('登录失败!') //登录失败则用户名为空
else
ShowMessage('登录成功!');
//分析HTML,取出每一条收藏贴子的URL,下载保存
finally
Params.Free;
end;
end;
楼上说的idhttp支持cookie 需要另一个控件。IdCookieManager1
我又一个下载网站网页的例子
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls, IdIntercept, IdLogBase, IdLogDebug,IniFiles;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
ListBox1: TListBox;
Edit1: TEdit;
Edit2: TEdit;
IdHTTP1: TIdHTTP;
Edit3: TEdit;
Edit4: TEdit;
Button2: TButton;
ComboBox1: TComboBox;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
inif:tinifile;
blink,link,endlink,artlink,endartlink,title,endtitle:string;
page:integer;
implementation
{$R *.dfm}
function CountSubStr(const SubStr,endsubstr, Source: string; var str:tstringlist): integer; // 注意,必须加const或者var关键字
var
i, n, len1, len2,m: Integer;
st:string;
begin
result := 0;
i := 1;
n:=0;
len1 := Length(SubStr);
len2 := Length(Source)-len1+1;
while (i <=len2)and (result<20) and (n>=0) do
begin
n := pos(SubStr, string(@Source)); // 这里的技巧是高效的关键, 直接将上一次找到的位置作为字符串的起始传入pos函数
m := pos(endsubstr,string(@Source[i+n])); //这样取字符串会产生问题///
if n > 0 then
begin
inc(result);
st:=copy(string(@Source),n+len1,m-len1);//length(endsubstr));//n+26,5);//这个是csdn的
str.Add(st);
inc(i, n+len1-1);
end
else break;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var astr:AnsiString;
str1:tstringlist;
i:integer;
st:string;
pagecount:integer;
begin
pagecount:=strtoint(edit2.text);
st:=edit1.Text;
str1:=tstringlist.Create;
for i:=1 to pagecount do
begin
astr:= idhttp1.Get(st+inttostr(i));
if countsubstr(artlink,endartlink,astr,str1) >0 then ;
end;
listbox1.Items:=str1;
for i:=0 to listbox1.Count-1 do
begin
memo1.Lines.Text:=idhttp1.Get('http://www.csdn.net/develop/Read_Article.asp?Id='+listbox1.Items.Strings);
memo1.Lines.SaveToFile('d:/'+listbox1.items.Strings+'.htm');
end;
//showmessage(copy(astr,pos('href="Read_Article.asp?Id=',astr)+26,5));
//copy(astr,pos('href="Read_Article.asp?Id=',astr),10)
//memo1.Lines.Text:=astr;
end;
procedure TForm1.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
//showmessage('');
end;
procedure TForm1.Button2Click(Sender: TObject);
var astr:AnsiString;
strl:tstringlist;
i:integer;
st:string;
pagecount:integer;
begin
pagecount:=strtoint(edit4.text);
st:=edit3.Text;
strl:=tstringlist.Create;
for i:=1 to pagecount do
begin
astr:= idhttp1.Get(st+inttostr(i));
if countsubstr(artlink,endartlink,astr,strl) >0 then;
end;
listbox1.Items:=strl;
for i:=0 to strl.Count-1 do
begin
memo1.Lines.Text:=idhttp1.Get('http://www.codelphi.com/channel/hjwz/read.asp?ano='+strl.Strings);
memo1.Lines.SaveToFile('d:/'+strl.Strings+'.htm');
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
inif:=tinifile.create('d:/htmchm.ini');
Inif.ReadSections(ComboBox1.Items);
combobox1.ItemIndex:=0;
{
inif.ReadSection();//读取一节的数据
inif.WriteString('csdn','sdfaa','asdfasdfasf');
inif.ReadString(csdn,)
inif.ReadSection('csdn',strs);
}
end;
procedure TForm1.Button3Click(Sender: TObject);
var astr:AnsiString;
str1:tstringlist;
i:integer;
st:string;
pagecount:integer;
begin
link:=inif.ReadString(combobox1.Text,'link','');//最后面的常量字符串做什么用?/a索引页连接
endlink:=inif.ReadString(combobox1.Text,'endlink','');//
artlink:=inif.ReadString(combobox1.Text,'artlink','');//文章标识连接
endartlink:=inif.ReadString(combobox1.Text,'endartlink','');
title:=inif.ReadString(combobox1.Text,'title','');//文章名称标题连接
endtitle:=inif.ReadString(combobox1.Text,'endtitle','');
page:=inif.Readinteger(combobox1.Text,'page',0);
blink:=inif.ReadString(combobox1.Text,'blink','');
//读取ini文件信息到对应的变量中//
pagecount:=page;
st:=link;
str1:=tstringlist.Create;
for i:=1 to pagecount do
begin
astr:= idhttp1.Get(st+inttostr(i));//st+inttostr(i)为对应的网址
try
if countsubstr(artlink,endartlink,astr,str1) >0 then ;
except
showmessage('去文件标号错误');
end;
end;
listbox1.Items:=str1;
for i:=0 to listbox1.Count-1 do
begin
//memo1.Lines.Text:=idhttp1.Post(blink+listbox1.Items.Strings,'co.txt');
memo1.Lines.Text:=idhttp1.Get(blink+listbox1.Items.Strings);
memo1.Lines.SaveToFile('d:/'+listbox1.items.Strings+'.htm');
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
idhttp1.Post('http://www.codelphi.com/channel/forum/showmessage.asp','co.txt',EmptyParam);
end;
end.