.NET、Windows 主机文件、通过代码覆盖 dns

发布于 2024-12-14 16:15:28 字数 966 浏览 3 评论 0原文

我正在开发一个项目,我们希望动态更改应用程序中发送请求的 IP 地址。例如,我们发送到 myWebsite.com 的第一个请求将发送至 192.168.123.1,第二个请求将发送至 192.168.123.5,第三个请求将发送至 192.168.123.10。

我知道可以通过位于 c:\windows\system32\drivers\etc 的 Windows 主机文件来实现此类功能,但这需要关闭应用程序、更改文件,然后再次打开应用程序。

是否有任何可用的 .Net 库/技术可以让我们从代码中动态覆盖 DNS?

谢谢 大卫,


我设法解决了这个问题。使用 VB.NET:

    Dim c As New WebClient
    c.Headers.Add("Host", "myWebsite.com")
    c.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2")
    c.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    c.Headers.Add("Accept-Language", "en-gb,en;q=0.5")
    c.Headers.Add("Accept-Encoding", "0")
    c.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")

    Dim s
    s = c.DownloadString("http://192.168.123.10/index.aspx")

设置 Host 标头告诉 IIS 请求的目标应用程序是哪个。这允许我们在 WebClient 对象的 URI 字符串中获得 IP 地址。

谢谢 大卫

I'm working on a project where we want to dynamically change the IP address of where requests are sent to within our app. So for example the first request we send to myWebsite.com goes to 192.168.123.1, the second we want to go to 192.168.123.5, the third to 192.168.123.10.

I know it is possible to achieve this type of functionality via the Windows hosts file located at c:\windows\system32\drivers\etc, however this would require closing the app, changing the file, then openning the app again.

Are there any .Net libraries/techniques available that would allow us to dynamically override the DNS from our code?

Thanks
David


I managed to work this out. Using VB.NET:

    Dim c As New WebClient
    c.Headers.Add("Host", "myWebsite.com")
    c.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2")
    c.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    c.Headers.Add("Accept-Language", "en-gb,en;q=0.5")
    c.Headers.Add("Accept-Encoding", "0")
    c.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")

    Dim s
    s = c.DownloadString("http://192.168.123.10/index.aspx")

Setting the Host header is telling IIS which application the request is targetting. This allows us to have the IP address in the URI string of the WebClient object.

Thanks
David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-12-21 16:15:31

我设法解决了这个问题。 (根据我上面的评论)。

I managed to work this out. (as per my above comment).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文