简体   繁体   中英

.NET, Windows host file, dns override via code

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM