Problems using HttpClient and Dns RRS feed

  • Question

  • I have some code that makes a http request to a server on a local network using HttpClient. It's time out is set to 5 seconds with:

    _client->Timeout = System::TimeSpan::FromSeconds(5); 


    auto urlFormat = gcnew String("http://{0}:22012/api/DatasetInformation/{1}/Identifier");
    auto task = _client->GetStringAsync(gcnew Uri(String::Format(urlFormat, Server, Database)));

    This works fine most of the time however on some networks we are finding that the call times out. Without a timeout the call will take up to 30 seconds. These have been networks where the server end is on a machine with IPv6 address (and possibly more than one network card)

    However if we force the DNS resolution up front with 

    IPAddress[] addresses = Dns.GetHostAddresses(server);
    var ipAddress = addresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();
    var urlFormat = "http://{0}:22012/api/DatasetInformation/{1}/Identifier";
    var urlString = string.Format(urlFormat, server, dbName);
    var uri = new Uri(urlString);
    HttpClient client = new HttpClient()
    var t = client.GetStringAsync(uri);

    The call succeeds almost instantly. We suspect that HttpClient is first attempting to make the http call using the IPv6 address and timing out before then succeeding with the IPv4 address.

    What's the best way of dealing with this problem ? If we use Dns.GetHostAddresses() how can we know which IP address is the 'good' one when there is more than one?

    Tuesday, September 17, 2019 4:00 AM

All replies

  • If you ping the server name on one of the slower machines do you get the ipv4 or ipv6 address?  I tend to think this should be resolved with dns entries instead code.
    Tuesday, September 17, 2019 10:47 AM
  • IPv6 is returned on a ping

    Thanks, I think you're right that this is a network misconfiguration issue rather than a code issue. I'm really not where I'd go to change DNS entries however.

    Tuesday, September 17, 2019 10:12 PM
  • Hi Patrick Henri Shirley, 

    Thank you for posting here.

    >> I'm really not where I'd go to change DNS entries however.

    Have your problem solved?

    If not, please provide more information about what you want to do.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, September 18, 2019 7:15 AM
  • You can try to clear the existing DNS entries.Here is a link with steps.

    I don't think there is something like bad or good IP address, it's depend on reachable or not.

    In any case, if you want to try with IPv4 only, then check an answer in a thread.

    Avik Das

    Wednesday, September 18, 2019 7:46 AM