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;