How to prevent DNS lookup when fetching by using HttpClient

丶灬走出姿态 提交于 2019-12-04 04:50:51

问题


I am not sure i am doing correctly or not

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

The host is : tatoeba.org

The url is : http://188.213.24.161/eng/sentences/show/1

Here screenshots

the url is given as above

the host is set as below


回答1:


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.



来源:https://stackoverflow.com/questions/42917528/how-to-prevent-dns-lookup-when-fetching-by-using-httpclient

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