System.Net.WebException: The remote name could not be resolved:
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);
}
}
}
}
System.Net.WebException remote name could not be resolved 'http'
You're calling the WebProxy(string, int)
constructor - where the string is meant to be a host name, not a URI.
You should call the WebProxy(string)
constructor, at which point the string is a URI. If you need to specify a non-default port, put that in the URI, e.g.
webRequest.Proxy = new WebProxy("https://10.1.1.1:12345");
ASP.NET Exception: The remote name could not be resolved: 'apiconnector.com'
I finally managed to get this working, with the help of a colleague. The problem only occurs in specific conditions, in my case, this was on my development machine as part of a company domain. The domain uses a proxy server to manage web requests/responses. It turns out that our proxy server was blocking responses from apiconnector.com hence the exception; In addition to that we had to adjust the proxy settings in Internet Explorer as this provides the default settings in Visual Studio too (when configured correctly).
I cannot specify what was changed in terms of the proxy settings, as I stated, I was helped by a colleague; he managed this part of the resolution; However that only solved half of the problem...the exception was still occurring with Visual Studio, however the addition of the following XML to the web.config file resolved everything, and now it works!
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>
</system.net>
Related Topics
Programmatically Encrypting a Config-File in .Net
Bind Multiple Combobox to a Single List - Issue: When I Select an Item, All Combo Boxes Change
Convert Newtonsoft.JSON.Linq.Jarray to a List of Specific Object Type
How to 'Await' Raising an Eventhandler Event
How to Get the Current Time of Day
Check If All Items Are the Same in a List
What Is the Command to Exit a Console Application in C#
Automatically Start a Windows Service on Install
The Source File Is Different from When the Module Was Built
Convert String to Int Array Using Linq
The State of Linkers for .Net Apps (Aka "Please Sir, May I Have a Linker" 2009 Edition)
Wpf Error 40 Bindingexpression Path Error: Property Not Found on 'Object'
Datetime to String with Time Zone
Log Queries Executed by Entity Framework Dbcontext
Practical Advantage of Generics VS Interfaces
No Definition Found for Getactiveobject from System.Runtime.Interopservices.Marshal C#