如何清除 System.Net 客户端 DNS 缓存?

编程入门 行业动态 更新时间:2024-10-24 16:28:35
本文介绍了如何清除 System.Net 客户端 DNS 缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更改 HOSTS 文件时正在使用 .NET WebRequest.我发现 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".我想单独定位其中的几个,因此我的程序将在向 example 发送请求之前在我的 HOSTS 文件中对机器特定的 IP 地址进行硬编码:

I have a number of servers load-balanced behind a single hostname, let's say 'example'. 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:

163.56.0.34  example

对于第一个服务器和第一个请求,这工作正常.然后我的程序再次更改了 HOSTS 文件:

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

163.56.0.48  example

然后我创建了一个新的 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.

使用断点和调试跟踪,我已经确认每次都将正确的值写入 HOSTS 文件.当我尝试从浏览器或其他程序访问 example 时,它会接受 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 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 的 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. 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;

当我解开我之前尝试过的所有奇怪的事情时,我发现了我需要的另一种设置以及上面的设置;在请求对象上,关闭keep-alive:

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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-26 23:22:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/670318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   客户端   System   Net   DNS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!