我使用以下代码从URL获取一些数据:
function GetUrlContent(const Url: string): string;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1024] of AnsiChar;
BytesRead: DWORD;
Size: Integer;
begin
Result := '';
NetHandle := InternetOpen('App', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if not Assigned(NetHandle) then Exit;
try
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if not Assigned(UrlHandle) then Exit;
try
{ Proceed with download }
Size := 0;
repeat
if not InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead) then Break;
if BytesRead = 0 then Break;
SetLength(Result, Size + BytesRead);
Move(Buffer, Result[Size + 1], BytesRead);
Inc(Size, BytesRead);
until False;
finally
InternetCloseHandle(UrlHandle);
end;
finally
InternetCloseHandle(NetHandle);
end;
end;
问题是,在我获取数据后,好像仍在建立连接,例如InternetCloseHandle()
崩溃或有故障。
返回结果后,我想关闭该函数的活动连接。
答案 0 :(得分:0)
感谢所有已解决的问题
InternetCloseHandle(hInternet);
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
我得到了答案,非常感谢
WinAPI: InternetCloseHandle function closes the handle but not the connection