L
LiChaoHui
Unregistered / Unconfirmed
GUEST, unregistred user!
用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.
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.