如何通过DotNetBrowser获取页面加载来源服务器的IP地址
Hey there! Let's break down how to grab those server IP addresses with DotNetBrowser—both for the main loaded page and individual resource requests. Here's what you need to know:
If you're after the IP address of the server that served the main page (the primary document), you can hook into the RequestCompleted event from the browser's Network module. This event fires every time a network request finishes, and we can filter to only care about the main frame's request.
Here's a code example to implement this:
// 假设你已经初始化了Browser实例 browser.Network.RequestCompleted += OnMainPageRequestCompleted; private void OnMainPageRequestCompleted(object sender, RequestCompletedEventArgs e) { // 只处理主框架的请求(对应页面本身的加载) if (e.Frame.IsMain && e.Response != null && e.Response.RemoteAddress != null) { string serverIp = e.Response.RemoteAddress.Address.ToString(); int serverPort = e.Response.RemoteAddress.Port; Console.WriteLine($"Main page served by: {serverIp}:{serverPort}"); } }
The IsMain check ensures we only pull the IP for the top-level page, not every script or image loaded alongside it. The RemoteAddress property gives you the actual IP address and port of the server that responded to the request.
If you need the IP for every individual resource request (like images, CSS, JavaScript files, or API calls), you can use the same RequestCompleted event—just skip the main frame filter. This lets you track which server handled each asset load.
Check out this implementation:
browser.Network.RequestCompleted += OnAllRequestsCompleted; private void OnAllRequestsCompleted(object sender, RequestCompletedEventArgs e) { if (e.Response != null && e.Response.RemoteAddress != null) { string requestUrl = e.Request.Url.ToString(); string serverIp = e.Response.RemoteAddress.Address.ToString(); Console.WriteLine($"Request for {requestUrl} handled by server IP: {serverIp}"); } }
Quick Notes:
- Make sure you're using a DotNetBrowser version that supports these properties (v2.x and above should work seamlessly).
- For HTTPS requests, this will give you the IP of the actual server that responded—even if it's a CDN node, not the origin domain's canonical IP.
- Always add null checks for
e.Responseande.Response.RemoteAddressto avoid errors if a request fails (e.g., 404, network timeout).
内容的提问来源于stack exchange,提问作者Bigtoe

