首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

求取网页源代码的步骤,InternetReadFile 除外

2013-09-05 
求取网页源代码的方法,InternetReadFile 除外?用 InternetReadFile 方法取网页源码有时会失效,有没有其他a

求取网页源代码的方法,InternetReadFile 除外?
用 InternetReadFile 方法取网页源码有时会失效,有没有其他api稳定性好的。
[解决办法]

引用:
符合我的要求,WinSock2单元引用的是哪个控件?

改成WinSock也行:
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinSock;
 
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;
  sa: TSockAddr;
  sHttpHead: array [0..1023] of Char;
  iReturn: Integer;
  buf: array [0..1023] of Char;
  s: string;
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;

  FillChar(sa, SizeOf(TSockAddr), 0);
  sa.sin_family := AF_INET;
  sa.sin_port := htons(80);
  sa.sin_addr.S_addr := inet_addr('202.108.22.5');
  if SOCKET_ERROR = connect(sktClient, sa, sizeof(TSockAddr)) 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 + #13 + #10;

  if SOCKET_ERROR = send(sktClient, sHttpHead, strlen(sHttpHead), 0) then Exit;

  s := '';
  repeat
    ZeroMemory(@buf, 1024);
    iReturn := recv(sktClient, buf, 1024, 0);
    s := s + StrPas(buf) + #13 + #10;
  until (iReturn <= 0) or (iReturn = SOCKET_ERROR);
 
  ShowMessage(s);
 
  closesocket(sktClient);
  WSACleanUp();
end;
 
end.

热点排行