Using the native WinInet API for Microsoft Windows:
function WebGetData(const UserAgent: string; const URL: string): string; var hInet: HINTERNET; hURL: HINTERNET; Buffer: array[0..1023] of AnsiChar; BufferLen: cardinal; begin result := ''; hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); if hInet = nil then RaiseLastOSError; try hURL := InternetOpenUrl(hInet, PChar(URL), nil, 0, 0, 0); if hURL = nil then RaiseLastOSError; try repeat if not InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) then RaiseLastOSError; result := result + UTF8Decode(Copy(Buffer, 1, BufferLen)) until BufferLen = 0; finally InternetCloseHandle(hURL); end; finally InternetCloseHandle(hInet); end; end;
Try:
procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text := WebGetData('My Own Client', 'http://www.bbc.co.uk') end;
But this only works if the encoding is UTF-8. Thus, to make it work in other cases, you either have to process them separately, or you can use Indy's high-level wrappers, as suggested by Marjan. I admit that they are superior in this case, but I still want to promote the basic API, if not for any other reason than educational ...
Andreas Rejbrand
source share