//大家看看这段代码是获取百度首页的网页源码,哪里需要改进啊,recv貌似一直在取……
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock2;type
TForm1 = class(TForm)
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormClick(Sender: TObject);
var
wVersionRequested: WORD;
wsaData: TWSAData;
sktClient: TSocket;
psa: PSockAddr;
sHttpHead: array [0..1023] of Char;
iReturn: Integer;
buf: array [0..1023] of Char;
begin
wVersionRequested := MakeWord(2, 0);
if 0 <> WSAStartup(wVersionRequested, wsaData) then Exit; sktClient := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if INVALID_SOCKET = sktClient then Exit; New(psa);
psa.sin_family := AF_INET;
psa.sin_port := htons(80);
psa.sin_addr.S_addr := inet_addr('202.108.22.5');
if SOCKET_ERROR = connect(sktClient, psa, sizeof(psa^)) then Exit;//showmessage('aa'); sHttpHead :=
'GET / HTTP/1.1' + #13 + #10 +
//'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*' + #13 + #10 +
//'Accept-Language: zh-cn' + #13 + #10 +
//'Accept-Encoding: gzip, deflate' + #13 + #10 +
//'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)' + #13 + #10 +
'Host: www.baidu.com' + #13 + #10 +
'Connection: Close' + #13 + #10; if SOCKET_ERROR = send(sktClient, sHttpHead, strlen(sHttpHead), 0) then Exit; repeat
iReturn := recv(sktClient, buf, 1024, 0);
until iReturn = SOCKET_ERROR; ShowMessage(buf); closesocket(sktClient);
WSACleanUp();
Dispose(psa);
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock2;type
TForm1 = class(TForm)
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormClick(Sender: TObject);
var
wVersionRequested: WORD;
wsaData: TWSAData;
sktClient: TSocket;
psa: PSockAddr;
sHttpHead: array [0..1023] of Char;
iReturn: Integer;
buf: array [0..1023] of Char;
begin
wVersionRequested := MakeWord(2, 0);
if 0 <> WSAStartup(wVersionRequested, wsaData) then Exit; sktClient := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if INVALID_SOCKET = sktClient then Exit; New(psa);
psa.sin_family := AF_INET;
psa.sin_port := htons(80);
psa.sin_addr.S_addr := inet_addr('202.108.22.5');
if SOCKET_ERROR = connect(sktClient, psa, sizeof(psa^)) then Exit;//showmessage('aa'); sHttpHead :=
'GET / HTTP/1.1' + #13 + #10 +
//'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*' + #13 + #10 +
//'Accept-Language: zh-cn' + #13 + #10 +
//'Accept-Encoding: gzip, deflate' + #13 + #10 +
//'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)' + #13 + #10 +
'Host: www.baidu.com' + #13 + #10 +
'Connection: Close' + #13 + #10; if SOCKET_ERROR = send(sktClient, sHttpHead, strlen(sHttpHead), 0) then Exit; repeat
iReturn := recv(sktClient, buf, 1024, 0);
until iReturn = SOCKET_ERROR; ShowMessage(buf); closesocket(sktClient);
WSACleanUp();
Dispose(psa);
end;end.
解决方案 »
- InfoPower版本
- 如何在16位操作系统下面(WIN3.1)使用压缩功能?如何重新整理zlib?
- 说一下OnKeyDown,OnKeyPress,OnKeyUp事件中的参数Key,不需要的也请帮忙up一下
- 如何提高读写文本文件的效率?
- 为什么客户端不能使用多线程呢?
- 树的单元怎样做才有效呢?
- 我要向WORD中插入图片,并要把图片设置成“文字四周缠绕”,怎么解决,高分报答
- 请问如何限制一个输入数字的长度?
- (100分)问:如何获取ASP网页上的内容
- 哪位大哥知道XPMan的问题????
- TComboBox---Text值----字符串翻转问题(急)
- 看到.net版服真人秀,收到delphi的啥时真人秀?
HTTP/1.1 200 OK
Cache-Control: private
Connection: close
Date: Thu, 06 Aug 2009 07:13:04 GMT
Content-Length: 10
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Set-Cookie: ASPSESSIONIDCSQQQQSC=IACNBBEBGGJJHDPAMFDHILBP; path=/ 0123456789 //怎么取0123456789,总不能一下就返回一大堆,有什么函数或API吗
是返回这样的吗
你在sktClient := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
创建一个socket,向'202.108.22.5'发tcp包,等待服务器返回,这个包是发给...
返回的值 是什么
如果一直读取的,那自然是没有返回错误的代码,否则返回的就是接收到的字节数了
他能接受TCP包?
1、正如10L所說,http頭即sHttpHead最后要用'Connection: Close' + #13 + #10 + #13 + #10;要兩對回車換行。2、recv函數:
Return ValuesIf no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
所以改成:
//var s: String;
s := '';
repeat
ZeroMemory(@buf, 1024);
iReturn := recv(sktClient, buf, 1024, 0);
s := s + StrPas(buf) + #13 + #10;
until (iReturn <= 0) or (iReturn = SOCKET_ERROR);