.NET、Windows 主机文件、通过代码覆盖 dns
我正在开发一个项目,我们希望动态更改应用程序中发送请求的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法解决了这个问题。 (根据我上面的评论)。
I managed to work this out. (as per my above comment).