如何清除System.Net客户端DNS缓存? [英] How do I clear System.Net client DNS cache?

查看:211
本文介绍了如何清除System.Net客户端DNS缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.NET的WebRequest而改变我的HOSTS文件。我观察到System.Net不接受这些变化? - 我怎么可以把它做这样

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?

我公司拥有一批服务器后面的单主机负载平衡的,让我们说'example.com'。我想针对其中几个独立,所以我计划将硬code在我的HOSTS机器特定的IP地址将请求发送到example.com之前文件:

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:

163.56.0.34  example.com

对于第一个服务器,并第一个请求,这工作正常。然后,我的计划再次改变HOSTS文件:

For the first server and first request, this works fine. Then my program changes the HOSTS file again:

163.56.0.48  example.com

和我创建一个新的HttpWebRequest。当我把这个的时候,我可以在NETMON观察到它去,而不是预期的第二个到第一个IP地址(163.56.0.34)。

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.

使用断点和调试的痕迹,我已经验证了正确的值不会被写入到主机每次文件。当我试图从一个浏览器或其他程序访问example.com,它确实兑现了HOSTS文件,然后进入第二个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.

使用NETMON我验证请求直接将显示的IP地址;没有HTTP代理

Using NETMON I've verified that requests are going directly to the IP address shown; there is no HTTP proxy.

因为其他一切都尊重改变HOSTS文件,我强烈怀疑System.Net基础设施缓存example.com的DNS主机IP关联。但是,我找不到任何参考这个缓存,并没有什么别的方法来冲洗,或将其关闭。

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.

我欢迎指令处理缓存,还有什么可能会引起这些症状,或者其他提出的诊断步骤的建议,可能是有益的。

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.

推荐答案

我终于挖出了从MSDN晦涩的命令,修复这样的:

I finally dug up the obscure command from MSDN that fixes this:

ServicePointManager.DnsRefreshTimeout = 0;

当我解开所有的奇怪的事情我已经试过previously,我发现一个,我需要与上面的另一个设置;请求对象上,关闭保活:

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:

request.KeepAlive = false;

这篇关于如何清除System.Net客户端DNS缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆