PC6下载站

分类分类

将网页读入字符串中的函数

关注+2009-09-13作者:

function UrlGetStr(const URL: string; ShowHeaders: boolean = false): string;
const
   Agent = 'Internet Explorer 6.0';
var
   hFile, HInet: HINTERNET;
   Buffer: array[0..32767] of Char;
   BufRead: Cardinal;
   BufSize: Cardinal;
   TempStream: TStringStream;
   dwIndex: dword;
begin
   HInet := InternetOpen(PChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
   if Assigned(HInet) then
   try
     if LowerCase(Copy(URL,1,7)) <> 'http://' then
     hFile := InternetOpenUrl(HInet, PChar('http://' + URL), nil, 0, 0, 0)
     else
     hFile := InternetOpenUrl(HInet, PChar(URL), nil, 0, 0, 0);
     TempStream := TStringStream.Create('');
     dwIndex := 0;
     BufSize := SizeOf(Buffer);
     HttpQueryInfo(hfile, HTTP_QUERY_RAW_HEADERS_CRLF, @Buffer, BufSize, dwIndex);
     if ShowHeaders then TempStream.Write(Buffer, BufSize);
     if Assigned(hFile) then
     try
       with TempStream do
       try
         while InternetReadFile(hFile, @Buffer, BufSize, BufRead) and (BufRead > 0) do
           Write(Buffer, BufRead);
         Result := DataString;
       finally
         Free;
       end;
     finally
       InternetCloseHandle(hFile);
     end;
   finally
     InternetCloseHandle(hinet);
   end;
end;
 

展开全部

相关文章

更多+相同厂商

热门推荐

  • 最新排行
  • 最热排行
  • 评分最高
排行榜

    点击查看更多

      点击查看更多

        点击查看更多

        说两句网友评论

          我要评论...
          取消