偶用的是如下的函数,但是线程数大的时候也会出现cpu点有率高的情况
functiondo
wnloadWithInet(const AUrl: string;
ACookie: String=''): string;
procedure Add(Buf: PChar;
Count: Integer);
var
Len: Integer;
begin
Len := Length(Result);
SetLength(Result, Len + Count);
Move(Buf^, Result[Len + 1], Count);
end;
function PrepareURL: string;
begin
Result := UpperCase(Copy(AUrl, 1, 7));
if Result <> 'HTTP://' then
Result := 'http://' + AUrl
else
Result := AUrl;
end;
var
BytesRead: DWORD;
Session, Connection: HINTERNET;
Buffer: array[1..1024] of Char;
boOK: Boolean;
proxyinfo: PInternetProxyInfo;
slCookie: TStringList;
i: Integer;
sCookieName, sCookieData: String;
begin
Result := '';
if AUrl = '' then
Exit;
if not GPory then
Session := InternetOpen(nil, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0)
else
Session := InternetOpen(nil, INTERNET_OPEN_TYPE_PROXY,
pchar(GPoryAddress + ':' + GPoryPort), '', 0);
if not Assigned(Session) then
raise Exception.Create(SysErrorMessage(GetLastError));
try
if ACookie <> '' then
begin
slCookie := TStringList.Create;
Split(aCookie, ';', slCookie);
for i := 0 to slCookie.Count -1do
begin
CookieSplit(slCookie.Strings, '=', sCookieName, sCookieData);
InternetSetCookie(PChar(PrepareURL), pChar(sCookieName), pChar(sCookieData));
end;
slCookie.Free;
end;
Connection := InternetOpenUrl(Session, PChar(PrepareURL), nil, 0,
INTERNET_FLAG_RAW_DATA, {INTERNET_FLAG_RELOAD, }0);
boOK := Assigned(Connection);
if not boOK then
Exit;
//if not Assigned(Connection) then
//raise;
//raise Exception.Create(SysErrorMessage(GetLastError));
try
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(Connection, @Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
Add(@Buffer, BytesRead);
Application.ProcessMessages;
until BytesRead = 0;
finally
InternetCloseHandle(Connection);
end;
finally
InternetCloseHandle(Session);
end;
end;