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

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

问题描述

我不确定我是否正常进行

I am not sure i am doing correctly or not

下面将阻止 DNS查找 keep-alive 设置为false?

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

主机是: tatoeba.org

url是: http://188.213.24.161/eng/sentences/show/1

这里截图

该网址以上方发布

主机设置为

推荐答案

p>我相信如果你指定你的主机作为一个ip地址(如你所做),那么.net将跳过dsn查找(不管保持活着还是主机头设置)。

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不会尝试解析IPAddresses。

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;
}

我看到的确是一个包含主机名的网址.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天全站免登陆