HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)
Solution 1
You can solve it by translating the host name to ip and add the host name to Headers
collection or to Host
property.
If your url is http://example.com/uri
. Resolve the host yourself. Suppose its 1.2.3.4
. It'll be http://1.2.3.4/uri
. Now add Host: example.com
header to your request. I think it can be done by setting HttpWebRequest.Host
property.
Solution 2
I know this is an old post, but was facing the same error, so thought to share the solution.
- The best solution I found, when that exception occurs while the Wifi is connected, is just to retry my server call with a slight sleep in between. It works most of the time, otherwise if the second call fails I cancel the request.
- This error can also raise if the user's Wifi is very unstable or the signal is very low. The same error occurs if there is no internet connection at all, even if connected to Wifi.
This is in line with my ans on :
Solution 3
Well I use the HttpClient - but it might be a similar problem. I had the same issue on a Android device (it worked on a Windows Phone)... But after I added the Host to the header it worked!
client.DefaultRequestHeaders.Host = "mydomain.com";
You can still use the name in the url (you don't have to use the IP address)
antfx
Originally from the north east of England and currently based in Valencia, Spain I have spent the last 16 years developing high-end, scalable computer systems across numerous sectors including investment banking, oil & gas, internet security, business analytics and online media.
Updated on October 05, 2020Comments
-
antfx over 3 years
I have a .NET program running on Ubuntu via Mono 2.10
The program downloads a webpage via an HttpWebRequest every minute or so which works fine most of the time:
String result; WebResponse objResponse; WebRequest objRequest = System.Net.HttpWebRequest.Create(url); using (objResponse = objRequest.GetResponse()) { using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } }
The problem is that after few days I start getting exceptions thrown:
DateTime: 01/25/2012 08:15:41 Type: System.Net.WebException Error: Error: NameResolutionFailure Stack: at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0 at socks_server.Program.readHtmlPage (System.String url) [0x00000] in <filename unknown>:0 at socks_server.Program.getAccessKeysProc () [0x00000] in <filename unknown>:0
The server is still abel to resolve DNS, for example
wget http://www.google.com
Will return the file without any probelm as will ping and other commands that resolve DNS.
My program however will continue to throw that exception until I restart it. After restarting the application it will start working again as it should.
I have checked open file counts on the system (400 ish), memory usage (327mb of 4gb), CPU usage (2-3%) and all are OK.
Any ideas?
-
antfx over 12 yearsYou mean lookup the IP before hand using
Dns.GetHostEntry(domainName);
or something? I assumed that would cause the same problem if it is really a DNS issue, or do you suspect it is a bug in Mono / .NET? -
Shiplu Mokaddim over 12 yearsYes, by programmatically or hard code. This makes sure
HttpWebRequest
don't resolve dns every time you make a request. -
antfx over 12 yearshmm, OK, not the ideal solution as it still leaves the problem there but I suppose I could make a call to
GetHostEntry
ever hour or so to check for updates in DNS and avoid the hard code. I'd really like to find the problem though as this only effects some of my servers, not all (I have 50) -
locrizak over 8 yearsChanging the wifi from my crappy work internet to tethering my LTE made this error go away.
-
Alex Sorokoletov over 8 yearsdo you have a reproducible sample?
-
Kim Rasmussen over 8 yearsNo. When the host have been set once, the setter can be removed... I think the device contains its own "DNS record" for future references. It didn't work before I added the line, but it worked after. When I tried to remove the line again, it worked... I don't have another device at the moment to be able to reproduce it. Note: I used a new created domain which the device have not visited before - and it is a completely different type of solution (Xamarin.Android).
-
Sagar Panwala about 8 yearsAnyone found the solution for this? I'm also facing the same problem and this host and ip work around is not working for me.
-
gtrujillos over 6 yearsNot sure, I was using only HttpClient.
-
Pierre over 6 yearsDid you set
request.KeepAlive = false;
? -
Lewis Hamill over 5 yearsNot the same issue as yours but the mention of WiFi made me realise what my problem was. My app was connected to an API local to our network and when I disconnected the WiFi it could no longer find the API.
-
Eric Legault about 5 yearsI've also tried retries with slight sleeps and it didn't eliminate the problem