如何编程登陆codelphi这个网站,并下载其内容,最好用indy 其它组件也可(100),上次来的朋友们表示感谢,同时需要领分(100分)

用ICS中的HttpCli吧,方法如下

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, HttpProt, ScktComp, StdCtrls;

type
TForm1 = class(TForm)
HttpCli1: THttpCli;
ServerSocket1: TServerSocket;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Text := Socket.ReceiveText;
Socket.Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Active := True;
end;

procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
ShowMessage('Listen on port: ' + IntToStr(ServerSocket1.Port));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
stm1, stm2: TMemoryStream;
s: String;
UserName, PassWd: String;
begin
UserName := 'yourname';
PassWd := 'yourpassword';
s := Format('txtUser=%s&txtpwd=%s&rempass=&name=&Login.x=1&Login.y=2',
[UserName, PassWd]);
stm1 := TMemoryStream.Create;
stm2 := TMemoryStream.Create;
stm1.Write(PChar(s)^, Length(s));
stm1.Position := 0;
HttpCli1.SendStream := stm1;
HttpCli1.RcvdStream := stm2;
HttpCli1.Reference := 'http://www.codelphi.com/';
HttpCli1.URL := 'http://www.codelphi.com/user/login.asp';
HttpCli1.Cookie := 'ASPSESSIONIDACRBDRSQ=FFLPJKPAPNHAFJFADJNMCLHE';
HttpCli1.Post;
stm2.Position := 0;
Memo1.Lines.LoadFromStream(stm2);
HttpCli1.SendStream := nil;
HttpCli1.RcvdStream := nil;
stm1.Free;
stm2.Free;
if Pos('累计积分', Memo1.Lines.Text) > 0 then
ShowMessage('登录成功')
else
ShowMessage('登录失败');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Memo1.Lines.Clear;
end;

end.
 
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object Button1: TButton
Left = 16
Top = 16
Width = 75
Height = 25
Caption = 'Open Proxy'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 16
Top = 56
Width = 505
Height = 105
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 1
end
object Button2: TButton
Left = 16
Top = 176
Width = 75
Height = 25
Caption = 'Do Login'
TabOrder = 2
OnClick = Button2Click
end
object HttpCli1: THttpCli
LocalAddr = '0.0.0.0'
ProxyPort = '80'
Agent = 'Mozilla/3.0 (compatible)'
Accept = 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*'
NoCache = False
ContentTypePost = 'application/x-www-form-urlencoded'
MultiThreaded = False
Left = 136
Top = 176
end
object ServerSocket1: TServerSocket
Active = False
Port = 5808
ServerType = stNonBlocking
OnListen = ServerSocket1Listen
OnClientRead = ServerSocket1ClientRead
Left = 144
Top = 16
end
end
 
本人实在愚蠢得很,有没有可以用indy 就能登陆的谢谢了分数另算
 
我已经调试通过,修正的后的代码在上面,我已经改过了
修改的部分是,加入了Cookie的数据
判断成功与否的方法如下

if Pos('累计积分', Memo1.Lines.Text) > 0 then
ShowMessage('登录成功')
else
ShowMessage('登录失败');

从界面上,我们知道,登陆成功后才能看到 '累计积分' 这几个字
没有登陆或登陆失败时是没有的
 
我测试过一些HttpClient的组件,有FastNet的, Indy的,ICS的
但我对ICS中的HttpClient最满意,因为在处理页面地址转向时,
ICS的HttpCli能够很好的处理,其它的都不行,
同时ICS是完全免费的!

其中包括了一些很有用的组件,稳定性和容错性都比较好,
所以ICS是首选,如果楼主不会安装,则要好好学学怎么安装组件包了
这可是,Delphi开发者的基本功啊
 
注意,用户名密码必须正确,自己替换掉源程序中的用户名和密码!!!
 
D6自带的那个例子就可以, 你运行Demo/Indy/HttpClient下面的例子, 按照下面的步骤操作
1 URL里输入 http://www.codelphi.com/user/login.asp
2 Post Information 里面输入 (红色部分用你自己的, 我用我的试了没问题)
txtUser=[red]用户名[/red]&txtpwd=[red]密码[/red]&rempass=&name=&Login.x=1&Login.y=2
3 点Go!按钮
4 URL里输入 http://www.codelphi.com/
5 点Go!按钮
 
to:LiChaoHui.我的D6中的ics编译不过去,需要d7中的winsock
d6的winsock 不行,
但是d7中没有了serversocket.以后再试验
to:tseug,4 URL里输入 http://www.codelphi.com/
到这里出错,方法不允许。
停在HTTP.Post(cbURL.Text, mePostData.Lines, Response)
而且还是读不出其他的页来如:http://www.codelphi.com/channel/hjwz/read.asp?ano=497
打不开这个页面
 
奇怪了,我的ICS是可以的,我用的是D6
我的ICS是playicq上面查找的
 
那就怪了,我刚又试过,没问题啊,我的是D6
 
to:LiChaoHui PSockAddr未定义
结果查winsock中果然没有,d7中的就有看来我的d6版本的问题,sp2已经装了,
一直用着呵呵,delphi6刚装的说实在的你的程序我不明白,我的cookie里面没有你
赋值的字符串呀'ASPSESSIONIDACRBDRSQ=FFLPJKPAPNHAFJFADJNMCLHE'
这也应该是基本的东西吧,我真的臭死了,赶紧找书看看,还有,
你的能下载里面的文章吗?我不知道是否废话。反正......
再谢谢各位了
 
to:LiChaoHui,
该用户已经登录,请先注销。
 
注意,那个Cookie是不能随便写的,
当你第一次访问http://www.codelphi.com/ 时,Web服务器会给你返回一个Cookie
从而开始一个会话,以后如果接收到Cookie,你就保存到字符串链表中
每次访问别的页面时,同时发送此Cookie
所以,你要下载一个文章的方法和步骤如下:
1。先访问http://www.codelphi.com/ 取得一个Session的Cookie
2。使用刚才获得的Cookie发出登录的请求,并判断请求是否成功
如果接受到新的Cookie,则保存或替换掉原来的
3。获取你关心的文章的列表,然后对Html文件进行解析,获取每个文章的链接地址
4。进行文章下载

对于此类功能,可以做成一个统一的模式,登录各种网站,
但对于用JavaScript进行加密控制登录的,这种办法就没有用了
这时候可以使用WebBrowser控件来完成登陆,并复制它的Cookie
然后可以用HttpClient来进行下载
 
谢谢各位了分分,哪个少的可以还跟我要。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
817
import
I
顶部