Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.WebException: The remote name could not be resolved:

I am testing an endpoint that I am experiencing some issues with.

I am simply using HttpClient in a loop that performs a request each hour.

var httpClient = new HttpClient(); var message = httpClient.GetAsync(url).Result; Console.WriteLine(message.StatusCode); 

Once in a while I am getting this exception:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The remote name could not be resolved: 'xxx'

The experience is that right after the exception, the URL can be accessed. In a browser you simply refresh the page and all is good.

I still haven't got any reports from users experiencing it so I am wondering if it's just a local issue here but could use a little information to help diagnose.

Is there a way to check if The remote name could not be resolved is caused by an DNS issue or by a web server issue from the exceptions? Can I get more information out of HttpCLient or do I need more advanced diagnostic tools?

like image 277
Poul K. Sørensen Avatar asked Jul 29 '14 11:07

Poul K. Sørensen


People also ask

How do I fix the remote name could not be resolved?

If your DNS is not resolving for internet addresses, a good first step is to reboot your router and modem as that can often resolve such issues.

How do I resolve a remote name?

Open the "Hosts" file in Notepad. On an new line, press the TAB key and enter the IP address of the Remote Server, then press TAB again and enter the name of the server. Save the file in the same file format (make sure it is not saved as a txt file).

What does remote name could not be resolved mean?

This process is called "Resolving". You can think of DNS as the phonebook of the Internet. When you HTTP request throws an exception, telling you that the remote name could not be resolved, it basically means that an IP address couldn't be solved from the provided domain name.

What does System Net WebException mean?

The WebException class is thrown by classes descended from WebRequest and WebResponse that implement pluggable protocols for accessing the Internet. When WebException is thrown by a descendant of the WebRequest class, the Response property provides the Internet response to the application.


2 Answers

It's probably caused by a local network connectivity issue (but also a DNS error is possible). Unfortunately HResult is generic, however you can determine the exact issue catching HttpRequestException and then inspecting InnerException: if it's a WebException then you can check the WebException.Status property, for example WebExceptionStatus.NameResolutionFailure should indicate a DNS resolution problem.


It may happen, there isn't much you can do.

What I'd suggest to always wrap that (network related) code in a loop with a try/catch block (as also suggested here for other fallible operations). Handle known exceptions, wait a little (say 1000 msec) and try again (for say 3 times). Only if failed all times then you can quit/report an error to your users. Very raw example like this:

private const int NumberOfRetries = 3; private const int DelayOnRetry = 1000;  public static async Task<HttpResponseMessage> GetFromUrlAsync(string url) {     using (var client = new HttpClient()) {         for (int i=1; i <= NumberOfRetries; ++i) {             try {                 return await client.GetAsync(url);              }             catch (Exception e) when (i < NumberOfRetries) {                 await Task.Delay(DelayOnRetry);             }         }     } } 
like image 108
Adriano Repetti Avatar answered Oct 05 '22 11:10

Adriano Repetti


I had a similar issue when trying to access a service (old ASMX service). The call would work when accessing via an IP however when calling with an alias I would get the remote name could not be resolved.

Added the following to the config and it resolved the issue:

<system.net>     <defaultProxy enabled="true">     </defaultProxy> </system.net> 
like image 31
user1620050 Avatar answered Oct 05 '22 13:10

user1620050