深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 673|回复: 0
打印 上一主题 下一主题

delphi通过api获得网页代码, getWebPae

[复制链接]

104

主题

171

帖子

1177

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1177
跳转到指定楼层
楼主
发表于 2019-7-25 22:17:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. uses WinInet;

  2. function TForm1.getWebPage(url: string): string;
  3. var
  4.   Session,
  5.   HttpFile:HINTERNET;
  6.   szSizeBuffer:Pointer;
  7.   dwLengthSizeBuffer:DWord;
  8.   dwReserved:DWord;
  9.   dwFileSize:DWord;
  10.   dwBytesRead:DWord;
  11.   Contents:PChar;
  12. begin
  13.   Session:=InternetOpen('',0,niL,niL,0);
  14.   HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,0,0);
  15.   dwLengthSizeBuffer:=1024;
  16.   HttpQueryInfo(HttpFile,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
  17.   GetMem(Contents,dwFileSize);
  18.   ZeroMemory(Contents,dwFileSize);   //fix by syant -- win10下可能有垃圾数据
  19.   InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
  20.   InternetCloseHandle(HttpFile);
  21.   InternetCloseHandle(Session);
  22.   Result:=StrPas(Contents);
  23.   FreeMem(Contents);
  24. end;
复制代码


而后在crc_notice中的实现是:
  1. begin
  2. try
  3.   Session:=InternetOpen('',0,niL,niL,0);
  4.   HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,INTERNET_FLAG_RELOAD,0);
  5.   //dwLengthSizeBuffer:=sizeof(Buffer);
  6.   //HttpQueryInfo(HttpFile,HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER,@Buffer,dwLengthSizeBuffer,dwReserved);
  7.   dwFileSize:=1024;
  8.   GetMem(Contents,dwFileSize);
  9.   ZeroMemory(Contents, dwFileSize);
  10.   InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
  11.   InternetCloseHandle(HttpFile);
  12.   InternetCloseHandle(Session);
  13.   Result:=StrPas(Contents);
  14.   if((length(result)>dwBytesRead) and (dwBytesRead>0))then result:=copy(result,1,dwBytesRead);
  15.   FreeMem(Contents);
  16. except
  17.   result:='00';
  18. end;
复制代码

不知道为什么不能获得文件大小!


最后的解决方案:

  1. function TForm1.UrlGetStr(const URL: string): string;
  2. const
  3.   Agent = 'Internet Explorer 6.0';
  4. var
  5.   hFile, HInet: HINTERNET;
  6.   Buffer: array[0..32767] of Char;
  7.   BufRead: Cardinal;
  8.   BufSize: Cardinal;
  9.   TempStream: TStringStream;
  10.   dwIndex: dword;
  11. begin
  12.   HInet := InternetOpen(PChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  13.   if Assigned(HInet) then
  14.   try
  15.     hFile := InternetOpenUrl(HInet, PChar(URL), nil, 0, 0, 0);
  16.     TempStream := TStringStream.Create('');
  17.     dwIndex := 0;
  18.     BufSize := SizeOf(Buffer);
  19.     //HttpQueryInfo(hfile, HTTP_QUERY_RAW_HEADERS_CRLF, @Buffer, BufSize, dwIndex);
  20.     //if ShowHeaders then TempStream.Write(Buffer, BufSize);
  21.     if Assigned(hFile) then
  22.     try
  23.       with TempStream do
  24.       try
  25.         while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
  26.           Write(Buffer, BufRead);
  27.         Result := DataString;
  28.       finally
  29.         Free;
  30.       end;
  31.     finally
  32.       InternetCloseHandle(hFile);
  33.     end;
  34.   finally
  35.     InternetCloseHandle(hinet);
  36.   end;
  37. end;
复制代码


结论:都是没有内存泄漏,都是用时0~16毫秒!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 02:08 , Processed in 0.036946 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表