使用 HttpClient 获取时如何防止 DNS 查找 [英] How to prevent DNS lookup when fetching by using HttpClient

查看:40
本文介绍了使用 HttpClient 获取时如何防止 DNS 查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我做得对不对

keep-alive 设置为 false 时,下面的方法是否会阻止 DNS 查找?

Would below way prevent DNS lookup when keep-alive is set false?

主持人是:tatoeba.org

网址是:http://188.213.24.161/eng/sentences/show/1

这里是截图

网址是以上

主机设置为如下

推荐答案

我相信如果你将你的主机指定为一个 ip 地址(就像你所做的那样),那么 .net 将跳过 dsn 查找(不管 keep alive或主机头设置).

I believe that if you specify your host as an ip address (as you did), then .net will skip the dsn look up (regardless of the keep alive or the host header setting).

如果您深入研究 HttpClient,您会发现它基本上使用 HttpWebRequest 来发出请求.https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpClient.cs

If you dig a little bit into HttpClient you will see it basically uses HttpWebRequest for making the requests. https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpClient.cs

HttpWebRequest 最终使用一个叫做 ServicePoint 的类,它调用一个Dns.TryInternalResolve.

HttpWebRequest eventually uses a class called ServicePoint which call a Dns.TryInternalResolve.

Dns.TryInternalResolve 不会尝试解析 IP 地址.

Dns.TryInternalResolve doesn't try to resolve IPAddresses.

有关更多信息,请参阅:https://referencesource.microsoft.com/#System/net/System/Net/DNS.cs,f8023b9c19212708

For more info refer to: https://referencesource.microsoft.com/#System/net/System/Net/DNS.cs,f8023b9c19212708

我还尝试通过运行以下几行并使用 netmon 监视请求来验证这一点

I also tried to verify that by running the following lines and monitor the requests using netmon

using (HttpClient c = new HttpClient())
{
    var response = c.GetAsync(url).Result;
}

我看到确实对于包含主机名的 url .net 发出 dns 请求,而对于以 ipAddress 作为主机名的请求,则没有 dns 请求.

I saw that indeed for a url that contains an host name .net issue a dns request while for requests with an ipAddress as an host name there is no dns request.

这篇关于使用 HttpClient 获取时如何防止 DNS 查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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