Oct 9, 2017 12:28 PM
(This post was last modified: Oct 9, 2017 09:41 PM by tcsh.)
The following allows you to use proxy's in your TWebBrowser component when using it in your projects.
Function source code:
Code
{$DEFINE DELPHI2009_UP}
function SetProxy(Server: String): Boolean;
// Server z.B. '127.0.0.1:8080' oder ''
type
INTERNET_PER_CONN_OPTION = record
dwOption: DWORD;
Value: record case Integer of 1: (dwValue: DWORD);
2: (pszValue: {$IFDEF DELPHI2009_UP}PWideChar{$ELSE}PAnsiChar{$ENDIF});
3: (ftValue: TFileTime);
end;
end;
LPINTERNET_PER_CONN_OPTION = ^INTERNET_PER_CONN_OPTION;
INTERNET_PER_CONN_OPTION_LIST = record dwSize: DWORD;
pszConnection:
LPTSTR;
dwOptionCount:
DWORD;
dwOptionError:
DWORD;
pOptions:
LPINTERNET_PER_CONN_OPTION;
end;
LPINTERNET_PER_CONN_OPTION_LIST = ^INTERNET_PER_CONN_OPTION_LIST;
const
INTERNET_PER_CONN_FLAGS = 1;
INTERNET_PER_CONN_PROXY_SERVER = 2;
INTERNET_PER_CONN_PROXY_BYPASS = 3;
INTERNET_PER_CONN_AUTOCONFIG_URL = 4;
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;
PROXY_TYPE_DIRECT = $00000001;
PROXY_TYPE_PROXY = $00000002;
PROXY_TYPE_AUTO_PROXY_URL = $00000004;
PROXY_TYPE_AUTO_DETECT = $00000008;
INTERNET_OPTION_REFRESH = 37;
INTERNET_OPTION_PER_CONNECTION_OPTION = 75;
INTERNET_OPTION_SETTINGS_CHANGED = 39;
var
OptionsList: INTERNET_PER_CONN_OPTION_LIST;
BufSize: DWORD;
HInternet: Pointer;
Agent: String;
begin
Result := False;
BufSize := SizeOf(OptionsList);
OptionsList.dwSize := BufSize;
OptionsList.pszConnection := nil;
OptionsList.dwOptionCount := 3;
OptionsList.pOptions := AllocMem(3 * SizeOf(INTERNET_PER_CONN_OPTION));
try
if not Assigned(OptionsList.pOptions) then
EXIT;
OptionsList.pOptions^.dwOption := INTERNET_PER_CONN_FLAGS;
OptionsList.pOptions^.Value.dwValue := PROXY_TYPE_DIRECT or
PROXY_TYPE_PROXY;
inc(OptionsList.pOptions);
OptionsList.pOptions^.dwOption := INTERNET_PER_CONN_PROXY_SERVER;
OptionsList.pOptions^.Value.pszValue := PChar(Server);
inc(OptionsList.pOptions);
OptionsList.pOptions^.dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
OptionsList.pOptions^.Value.pszValue := 'local';
dec(OptionsList.pOptions, 2);
Agent := ExtractFileName(Application.ExeName);
HInternet := InternetOpen
({$IFDEF DELPHI2009_UP}PWideChar{$ELSE}PAnsiChar{$ENDIF}
(Agent), INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
try
Result := InternetSetOption(HInternet,
INTERNET_OPTION_PER_CONNECTION_OPTION, @OptionsList, BufSize);
InternetSetOption(HInternet, INTERNET_OPTION_REFRESH, nil, 0);
finally
InternetCloseHandle(HInternet);
end;
finally
FreeMem(OptionsList.pOptions);
end;
end;Now just make sure to call the above function before using WebBrowser.Navigate , like this:
Code
begin
SetProxy('ip:port');
WebBrowser1.Navigate('https://hackforums.net');
end;or if you want to load a specific proxy from a specific line located, for example, in a file named proxy.txt in your application main dir, use this:
Code
var
proxy: TStringList;
begin
proxy := TStringList.Create;
proxy.LoadFromFile('proxy.txt');
SetProxy(proxy.Strings[0]); //will load the proxy from the 1st line in the .txt
proxy.Free;
end;That's it.
The component will set the specified proxy, either from the list or directly, in accordance to the route you chose to use, then it will navigate to the specified url. Obviously you can use the function on multiple instances of TWebBrowser, provided they're on your form or self-created. Just attach a new SetProxy(); to each instance and specify the proxy details (ip:port or line from file).
**************************************************************************************************************
Known issues:
When trying to display, for example, captcha (or more recently, reCaptcha) on a TWebBrowser component in your application, it will most likely fail due to compatibility problems, as the component is using the old IE.
This has an easy fix:
Drop a TButton and name it Registry.
Double click it and add the following code:
Code
procedure TForm1.RegistryClick(Sender: TObject);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION', TRUE);
Registry.WriteInteger('YourApplicationExeName', 11001);
Registry.Free;
ShowMessage('Registered!');
end;Just replace YourApplicationExeName with the name of your application.
Once you run the application for the first time, click the button named Registry. This will create a registry entry on the current PC (doesn't require Administrator rights) and registers the TWebBrowser component to use the latest IE (11).
Obviously the above code needs to be ran only once per system - since it creates a registry entry you don't need to do it every single time you use the application.
Voila, you're browsing with Edge 🤭
**************************************************************************************************************
Bonus:
Instantly empty TWebBrowser cache, cookies, sessions and everything else. Useful to clear everything once you've used it to navigate and so on.
Code
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
begin
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
if hCacheDir <> 0 then begin
repeat
// delete cookies
if ((lpEntryInfo^.CacheEntryType and COOKIE_CACHE_ENTRY) = COOKIE_CACHE_ENTRY)
// delete history
or ((lpEntryInfo^.CacheEntryType and URLHISTORY_CACHE_ENTRY) = URLHISTORY_CACHE_ENTRY)
// delete "normal" cache
or ((lpEntryInfo^.CacheEntryType and NORMAL_CACHE_ENTRY) = NORMAL_CACHE_ENTRY)
then DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
FreeMem(lpEntryInfo, dwEntrySize);
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
end;
FreeMem(lpEntryInfo, dwEntrySize);
FindCloseUrlCache(hCacheDir);
end;Use the code in a procedure and call it once you're done.
This is useful for clearing everything.
For example: if you run your application and use it to Navigate and log in to say, a Facebook account, then just close the application and relaunch it again, you're still logged in. Similarly, it's independent from the IE installed on your system so clearing that doesn't help.
Using this code once you've completed everything ensures a fresh session with no trace on next launch. Obviously, there are others ways also, this is just one of many. Good luck 🤝


![[Image: F2G97kn.png]](https://i.imgur.com/F2G97kn.png)