I'm using the .NET WebRequest while changing my HOSTS file. I'm observing that System.Net doesn't honor those changes - how can I make it do so?
我在更改我的HOSTS文件时使用的是.NET WebRequest。我观察到System.Net不尊重这些变化 - 我怎么能这样做呢?
I have a number of servers load-balanced behind a single hostname, let's say 'example.com'. I want to target several of them individually, so my program will hard-code the machine-specific IP address in my HOSTS file before sending a request to example.com:
我在一个主机名后面有一些负载均衡的服务器,比方说'example.com'。我想单独定位其中几个,所以我的程序会在向example.com发送请求之前在我的HOSTS文件中硬编码机器特定的IP地址:
163.56.0.34 example.com
For the first server and first request, this works fine. Then my program changes the HOSTS file again:
对于第一个服务器和第一个请求,这很好。然后我的程序再次更改HOSTS文件:
163.56.0.48 example.com
And I create a new HttpWebRequest. When I send this one off, I can observe in NETMON that it goes to the first IP address (163.56.0.34) instead of the expected second one.
我创建了一个新的HttpWebRequest。当我发送这个时,我可以在NETMON中观察到它转到第一个IP地址(163.56.0.34)而不是预期的第二个IP地址。
Using breakpoints and debug traces, I've verified that the correct value does get written to the HOSTS file each time. When I attempt to access example.com from a browser or other program, it does honor the HOSTS file and go to the second IP address.
使用断点和调试跟踪,我已经验证每次都将正确的值写入HOSTS文件。当我尝试从浏览器或其他程序访问example.com时,它确实尊重HOSTS文件并转到第二个IP地址。
Using NETMON I've verified that requests are going directly to the IP address shown; there is no HTTP proxy.
使用NETMON我已经验证了请求是直接进入显示的IP地址;没有HTTP代理。
Since everything else is honoring the changed HOSTS file, I strongly suspect that the System.Net infrastructure has cached the DNS host-IP association for example.com. However, I can find no reference to this caching, and know of no way to flush it or turn it off.
由于其他一切都是为了兑现已更改的HOSTS文件,因此我强烈怀疑System.Net基础结构已缓存example.com的DNS主机-IP关联。但是,我找不到对此缓存的引用,并且知道无法刷新或关闭它。
I would welcome instructions for dealing with the cache, suggestions for what else might be causing these symptoms, or other proposed diagnostic steps that might be useful.
我欢迎处理缓存的说明,可能导致这些症状的其他建议,或其他可能有用的诊断步骤。
4 个解决方案
#1
37
I finally dug up the obscure command from MSDN that fixes this:
我终于从MSDN中挖出了修复此命令的obscure命令:
ServicePointManager.DnsRefreshTimeout = 0;
As I unwound all the weird things I'd tried previously, I discovered one other setting that I need along with the one above; on the request object, turn off keep-alive:
当我解开之前尝试的所有奇怪的东西时,我发现了另外一个我需要的设置和上面的设置;在请求对象上,关闭keep-alive:
request.KeepAlive = false;
#2
5
If you want to keep the DnsRefreshTimeout > 0 then you can update the cache by making a call to:
如果要保持DnsRefreshTimeout> 0,则可以通过调用以下内容来更新缓存:
System.Net.Dns.GetHostEntry("example.com");
#3
3
you could use System.Diagnostics.Process to launch ipconfig /flushdns
您可以使用System.Diagnostics.Process启动ipconfig / flushdns
#4
1
A late answer to be sure, but I found this solution worked better than the accepted answer. In my scenario I am testing SQL connections, and I couldn't apply the existing answer since I have no request.KeepAlive
to set.
一个迟到的答案可以肯定,但我发现这个解决方案比接受的答案更好。在我的场景中,我正在测试SQL连接,并且我无法应用现有的答案,因为我没有请求。要设置要保持活动状态。
This page by Brian Mancini ("derp turkey") details his adventure in clearing the DNS cache. Full props to him, I'm just adding his solution here:
本页由Brian Mancini(“derp turkey”)详细介绍了他清除DNS缓存的冒险经历。完全道具给他,我只是在这里添加他的解决方案:
public class DnsUtils
{
[DllImport("dnsapi.dll", EntryPoint="DnsFlushResolverCache")]
static extern UInt32 DnsFlushResolverCache();
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A")]
public static extern int DnsFlushResolverCacheEntry(string hostName);
public static void FlushCache()
{
DnsFlushResolverCache();
}
public static void FlushCache(string hostName)
{
DnsFlushResolverCacheEntry(hostName);
}
}
#1
37
I finally dug up the obscure command from MSDN that fixes this:
我终于从MSDN中挖出了修复此命令的obscure命令:
ServicePointManager.DnsRefreshTimeout = 0;
As I unwound all the weird things I'd tried previously, I discovered one other setting that I need along with the one above; on the request object, turn off keep-alive:
当我解开之前尝试的所有奇怪的东西时,我发现了另外一个我需要的设置和上面的设置;在请求对象上,关闭keep-alive:
request.KeepAlive = false;
#2
5
If you want to keep the DnsRefreshTimeout > 0 then you can update the cache by making a call to:
如果要保持DnsRefreshTimeout> 0,则可以通过调用以下内容来更新缓存:
System.Net.Dns.GetHostEntry("example.com");
#3
3
you could use System.Diagnostics.Process to launch ipconfig /flushdns
您可以使用System.Diagnostics.Process启动ipconfig / flushdns
#4
1
A late answer to be sure, but I found this solution worked better than the accepted answer. In my scenario I am testing SQL connections, and I couldn't apply the existing answer since I have no request.KeepAlive
to set.
一个迟到的答案可以肯定,但我发现这个解决方案比接受的答案更好。在我的场景中,我正在测试SQL连接,并且我无法应用现有的答案,因为我没有请求。要设置要保持活动状态。
This page by Brian Mancini ("derp turkey") details his adventure in clearing the DNS cache. Full props to him, I'm just adding his solution here:
本页由Brian Mancini(“derp turkey”)详细介绍了他清除DNS缓存的冒险经历。完全道具给他,我只是在这里添加他的解决方案:
public class DnsUtils
{
[DllImport("dnsapi.dll", EntryPoint="DnsFlushResolverCache")]
static extern UInt32 DnsFlushResolverCache();
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A")]
public static extern int DnsFlushResolverCacheEntry(string hostName);
public static void FlushCache()
{
DnsFlushResolverCache();
}
public static void FlushCache(string hostName)
{
DnsFlushResolverCacheEntry(hostName);
}
}