我把.pas文件贴出来吧!
unit sock;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
CSocket1: TClientSocket;
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure CSocket1Error(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure CSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
CSocket1.host:='localhost';
CSocket1.Port:=80;
CSocket1.Active:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
{var lst:tstringlist;
}
begin
CSocket1.Socket.SendText('GET / HTTP/1.1'+#13#10#13#10);
{lst:=tstringlist.create;
lst.add('GET / HTTP/1.1');
lst.add('Accept: application/vnd.ms-excel, application/msword, */*');
lst.add('Accept-Language: zh-cn');
lst.add('Accept-Encoding: gzip, deflate');
lst.add('User-Agent: Mozilla/4.0 (compatible;
MSIE 4.01;
Windows 98)');
lst.add('Host: localhost');
lst.add('Connection: Keep-Alive');
lst.add('');
//<b>千万别忘了多加一个回车!</b>
CSocket1.Socket.SendText(lst.text);
lst.free;
}
Memo1.Lines.Add(CSocket1.Socket.ReceiveText);
end;
procedure TForm1.CSocket1Error(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
CSocket1.Close;
end;
procedure TForm1.CSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(CSocket1.Socket.ReceiveText);
end;
end.