能否使用VB.NET通过TOR浏览器检测URL是否存在?
Great question! You’re right that regular browser controls work smoothly for clearnet checks, but routing traffic through Tor requires a bit of extra configuration. Here are three practical approaches to make this work in VB.NET:
1. Lightweight Approach: Use HttpClient with Tor SOCKS Proxy
This is my go-to for batch URL checks since it doesn’t require spinning up a full browser instance. Tor runs a SOCKS5 proxy by default—Tor Browser uses port 9150, while the standalone Tor Service uses 9050.
First, make sure Tor is running (either launch Tor Browser or start the Tor Windows Service). Then configure HttpClient to route traffic through the proxy:
Imports System.Net Imports System.Net.Http Public Async Function CheckUrlTorAsync(url As String) As Task(Of Boolean) Dim socksProxy = New WebProxy("socks5://127.0.0.1:9150") ' Use 9050 for standalone Tor Service Dim handler = New SocketsHttpHandler() With { .Proxy = socksProxy, .UseProxy = True, .AllowAutoRedirect = True, .ConnectTimeout = TimeSpan.FromSeconds(10) } Using httpClient = New HttpClient(handler) Try Dim response = Await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead) ' Return True if we get a 2xx or 3xx status code (indicates the server is reachable) Return response.IsSuccessStatusCode OrElse (CInt(response.StatusCode) >= 300 AndAlso CInt(response.StatusCode) < 400) Catch ex As HttpRequestException ' Handle connection timeouts, unreachable nodes, etc. Return False End Try End Using End Function
Notes:
- Add error handling for cases where Tor isn’t running (you’ll get a proxy connection failure).
- Adjust the timeout based on Tor’s typical connection speeds—sometimes nodes are slow!
2. Browser-Based Approach: Selenium with Tor Browser
If you need to mimic real browser behavior (e.g., handling JavaScript-rendered content), you can use Selenium to control Tor Browser directly.
First, install these NuGet packages:
Selenium.WebDriverSelenium.WebDriver.GeckoDriver
Then configure Selenium to launch Tor Browser:
Imports OpenQA.Selenium Imports OpenQA.Selenium.Firefox Public Function CheckUrlTorBrowser(url As String) As Boolean ' Path to your Tor Browser executable (adjust this to your installation path) Dim torBrowserPath = "C:\Users\YourUsername\Desktop\Tor Browser\Browser\firefox.exe" Dim geckoDriverPath = "C:\Path\To\geckodriver.exe" ' Download from Mozilla's geckodriver site Dim firefoxOptions = New FirefoxOptions() firefoxOptions.BinaryLocation = torBrowserPath ' Tor Browser already has proxy settings configured, so no need to set them here Using driver = New FirefoxDriver(geckoDriverPath, firefoxOptions) driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15) Try driver.Navigate().GoToUrl(url) ' Check if the page loaded successfully (you can add more checks here, e.g., for specific elements) Return True Catch ex As WebDriverTimeoutException Return False Catch ex As NoSuchWindowException ' Tor Browser might have closed unexpectedly Return False End Try End Using End Function
Notes:
- Download the correct geckodriver version matching your Tor Browser’s Firefox version.
- Tor Browser may block automated scripts by default—you might need to tweak its settings (e.g., lower security levels temporarily for testing).
3. Native Browser Control Approach: Configure WebBrowser to Use Tor Proxy
If you want to stick with the WebBrowser control (based on IE), you’ll need to force it to use Tor’s SOCKS proxy. This requires modifying system proxy settings temporarily (or using WinINet APIs to set proxy per-process).
Here’s a quick example using registry settings (note: this affects the entire system’s proxy, so reset it after!):
Imports Microsoft.Win32 Imports System.Runtime.InteropServices Public Sub SetTorProxyForWebBrowser(enable As Boolean) Dim registryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", True) If enable Then registryKey.SetValue("ProxyEnable", 1) registryKey.SetValue("ProxyServer", "socks=127.0.0.1:9150") registryKey.SetValue("Override", "<local>") ' Bypass proxy for local addresses Else registryKey.SetValue("ProxyEnable", 0) End If registryKey.Close() ' Refresh proxy settings InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0) InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0) End Sub ' Add these P/Invoke declarations at the top of your class <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function InternetSetOption(hInternet As IntPtr, dwOption As Integer, lpBuffer As IntPtr, dwBufferLength As Integer) As Boolean End Function Private Const INTERNET_OPTION_SETTINGS_CHANGED As Integer = 39 Private Const INTERNET_OPTION_REFRESH As Integer = 37
Notes:
- This approach is less ideal because it changes system-wide proxy settings—users might notice their regular browser routing through Tor unexpectedly.
- The
WebBrowsercontrol uses IE’s rendering engine, which might not support modern Tor Browser features.
Key Considerations
- Always ensure Tor is running before attempting checks—you can add a quick connection test to
127.0.0.1:9150to verify. - Tor nodes can be slow, so adjust timeouts generously.
- Some onion sites (or clearnet sites accessed via Tor) may block automated requests—consider adding delays or mimicking real user agents.
内容的提问来源于stack exchange,提问作者BCLtd

