求idhttp的post和get的多线程例子(100分)

  • 主题发起人 主题发起人 PROSE
  • 开始时间 开始时间
P

PROSE

Unregistered / Unconfirmed
GUEST, unregistred user!
因为直接用idhttp,获取网页时就会出现界面没反应的情况,用那个indy Misc下面的那个控件效果也不是很好,以后还会有多线程取得不同页面的情况,所以想找一下多线程的解决例子。就是多线程抓网页,下来分析
 
我也正想要这个
 
偶用的是如下的函数,但是线程数大的时候也会出现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;
 
后退
顶部