HttpClient // Timeout // The remote name could not be resolved

Noah Aas 545 Reputation points
2025-01-23T18:48:34.5966667+00:00

Hello,

The remote name could not be resolved

using (HttpClient client = new HttpClient())
{
	client.BaseAddress = new Uri(System.BaseAddress);
	client.Timeout = new TimeSpan(0, 0, System.Timeout);


	
public void Exception(Exception ex, string functionName, string url)
{
	var ext = ex.InnerException;
	while (ext != null)
	{
		ext = ext.InnerException;
	}
	throw new Exception($"Exception {functionName} = '{url}'\n \n{ex.Message}");
}

The connection must be established first. How long does that take? 10 seconds? Can I set that?

Timeout 2, when the connection is established, ask the question, no answer is sent back.

How can I solve the problem? What could be the reason? The error occurs sporadically. On average after 200 requests.

Exception InnerException='An error occurred while sending the request.'

Exception InnerException='The remote name could not be resolved: 'public.api.XXXX.com''

Is the cause on the server or on the client? I need to work out a solution, I don't have the experience. Are there any links where it can be found? The customer says all good on the server.

Who is right?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,579 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,244 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. AgaveJoe 29,676 Reputation points
    2025-01-23T23:04:23.4866667+00:00

    The error message "The remote name could not be resolved" means the domain could be be resolved to an IP which is usually a DNS issue.

    See if the following guidance documentation helps.

    https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines

    0 comments No comments

  2. Bruce (SqlWork.com) 70,216 Reputation points
    2025-01-23T23:10:49.19+00:00

    HttpClient has one timeout parameter defaulted to 100 seconds. this is max time for a request, and included the DNS resolve, connecting to the ipaddress returned from the resolve, sending the data, the server processing the data, the server sending the response, and the client receiving the response.

    "remote name can not be resolved" error means that when the client calls its DNS server, the DNS server can not find the domain defined. a DNS resolve should return in < 15 seconds. this error happens as soon as the resolve completes and is not a timeout.

    0 comments No comments

  3. SurferOnWww 3,811 Reputation points
    2025-01-24T01:16:51.9+00:00

    Exception InnerException='The remote name could not be resolved: 'public.api.XXXX.com''

    Try sending ping to the domain you are trying to access to see if the name resolution is properly done.

    I guess actual domain is not public.api.XXXX.com. Since it is actually existing, however, avoid use it as sample in your question.

    enter image description here


  4. AgaveJoe 29,676 Reputation points
    2025-01-24T16:49:49.2+00:00

    I provided a link with information about DNS and how it can affect HttpClient. The reference documentation also contains best practices with code examples. But, as far as I can tell you've made no changes to the code.

    Below is another article explains some of the issue you might have with HttpClient depending on your environment and implementation.

    https://dev.to/gkarwchan/calling-http-api-and-the-problem-of-socket-exhaustion-and-dns-rotation-555d

    How can I prove this? I think so too.

    Read the documentation, make some code changes depending on your implementation, and test.


  5. MotoX80 35,226 Reputation points
    2025-01-27T18:45:33.3966667+00:00

    to reach more people and their experience.

    What DNS servers are you using? A simple nslookup on a name would tell you if you could reach them or not. Even if the DNS servers were not reachable, the DNS client on your pc will cache the address. "Ipconfig /displaydns" will show you the cache entries.

    Here is a Powershell script that might help you. It does a nslookup every few seconds. Adjust the sleep time to suite your needs and kick it off and let it run. See if it encounters a problem at some point.

    If the script encounters an error, then that would indicate that you have some general network connectivity problem.

    while ($true) {
        $ms = Resolve-DnsName microsoft.com
        "{0} - Number of records = {1}" -f (get-date) , $ms.count
        start-sleep -Seconds 30
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.