How to prevent DNS lookup when fetching by using HttpClient

末鹿安然 提交于 2019-12-02 03:41:45

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).

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 eventually uses a class called ServicePoint which call a Dns.TryInternalResolve.

Dns.TryInternalResolve doesn't try to resolve IPAddresses.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!