How to load a webpage into a variable?

I don't care, it's a string, a string list, memo, etc., but not a disk file

How to load a competing webpage into a variable? Thanks

+7
source share
2 answers

Using Indy:

const HTTP_RESPONSE_OK = 200; function GetPage(aURL: string): string; var Response: TStringStream; HTTP: TIdHTTP; begin Result := ''; Response := TStringStream.Create(''); try HTTP := TIdHTTP.Create(nil); try HTTP.Get(aURL, Response); if HTTP.ResponseCode = HTTP_RESPONSE_OK then begin Result := Response.DataString; end else begin // TODO -cLogging: add some logging end; finally HTTP.Free; end; finally Response.Free; end; end; 
+17
source

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 ...

+14
source

All Articles