难道这里没有高手吗? (关于网络) 急急急 (20分)

  • 主题发起人 主题发起人 ts007
  • 开始时间 开始时间
T

ts007

Unregistered / Unconfirmed
GUEST, unregistred user!
form上有一个memo控件和一个edit控件
////////////////////
edit上输入网址后,按回车
能不能网页内容下载到momo中?
///////////////////////
最好能判断网页中的连接
/////////////////////////////////
谢谢大家
 
老兄你运气不错,我恰好在做这个。不过,看来你水平很高啊,回答的问题几乎是全分。

unit Unit1;

interface

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

type
TForm1 = class(TForm)
ClientSocket: TClientSocket;
btnLink: TButton;
btnSend: TButton;
Memo: TMemo;
Edit: TEdit;
procedure btnLinkClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnLinkClick(Sender: TObject);
begin
Memo.Lines.Clear;
ClientSocket.Open;
end;

procedure TForm1.btnSendClick(Sender: TObject);
begin
ClientSocket.Socket.SendText('GET /'+Edit.Text+#$0D#$0A);
end;

procedure TForm1.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
Var
s:String;
begin
If Socket.Connected Then
Begin
s:=Socket.ReceiveText;
Memo.Lines.Add(s);
End;
end;

procedure TForm1.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add('Connected!');
end;

end.

不过这样得到的内容有一部分不是网页,自己判断吧。
 
对 firstrose
朋友你的程序是不是有问题?

能不能详细一点!
 
用http控件,把网站上的内容下载到memo中不就搞定了?代码自己写吧.
 
没有问题。我试了。这已经是全部的代码了。下面是dfm,自己看。注意ClientSocket
的属性
object Form1: TForm1
Left = 192
Top = 114
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object btnLink: TButton
Left = 32
Top = 72
Width = 75
Height = 25
Caption = 'Link'
TabOrder = 0
OnClick = btnLinkClick
end
object btnSend: TButton
Left = 32
Top = 104
Width = 75
Height = 25
Caption = 'SendRequest'
TabOrder = 1
OnClick = btnSendClick
end
object Memo: TMemo
Left = 152
Top = 16
Width = 377
Height = 305
ScrollBars = ssBoth
TabOrder = 2
end
object Edit: TEdit
Left = 16
Top = 160
Width = 121
Height = 21
TabOrder = 3
Text = '/'
end
object ClientSocket: TClientSocket
Active = False
Address = '127.0.0.1'
ClientType = ctNonBlocking
Port = 80
OnConnect = ClientSocketConnect
OnRead = ClientSocketRead
Left = 32
Top = 24
end
end
 
demo里有个列子
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls,wininet, ExtCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
StatusBar1: TStatusBar;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
urlsite:string;
hinternet:pointer;
hopenurl:pointer;
bloop:boolean;
lpbuffer:array[0..2048] of char;
dwnumberofbytesread,all_time1,all_time2:dword;
sbuffer:string;
qianzhui:string;
begin
qianzhui:=copy(edit1.Text,0,7);
if (qianzhui<>'http://') then
edit1.Text:='http://'+edit1.Text;
all_time1:=gettickcount;
hinternet:=internetopen(pchar('application'),internet_open_type_preconfig,nil,nil,internet_flag_no_cache_write);
if (hinternet<>nil) then
begin
internetsetoption(hinternet,internet_option_connect_timeout,pointer(4000),4);
internetsetoption(hinternet,internet_option_connect_retries,pointer(5),1);
internetsetoption(hinternet,internet_option_send_timeout,pointer(4000),4);
internetsetoption(hinternet,internet_option_receive_timeout,pointer(4000),4);
internetsetoption(hinternet,internet_option_data_send_timeout,pointer(4000),4);
internetsetoption(hinternet,internet_option_data_receive_timeout,pointer(4000),4);
urlsite:=edit1.text;
hopenurl:=internetopenurl(hinternet,pchar(urlsite),nil,0,internet_flag_reload,$0);
if (hopenurl<>nil) then
begin
bloop:=true;
sbuffer:='';
while bloop do
begin
lpbuffer:='';
internetreadfile(hopenurl,@lpbuffer,sizeof(lpbuffer),dwnumberofbytesread);
if dwnumberofbytesread=0 then
bloop:=false
else
sbuffer:=sbuffer+strpas(lpbuffer);
end;
memo1.clear;
memo1.lines.add(sbuffer);
statusbar1.panels[0].text:='open url ok';
end
else
statusbar1.panels[0].text:='error';
end;
if (hopenurl<>nil) then internetclosehandle(hopenurl);
if (hopenurl<>nil) then internetclosehandle(hinternet);
all_time2:=gettickcount;
edit1.SetFocus;
label1.Caption:='完成时间为: '+ inttostr(all_time2-all_time1)+' ';
end;

end.
 
谢谢!!
 
多人接受答案了。
 
后退
顶部