15

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
  • 3,205
  • 3
  • 35
  • 45

5 Answers5

9

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.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 1
    You 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? – antfx Jan 25 '12 at 08:35
  • Yes, by programmatically or hard code. This makes sure `HttpWebRequest` don't resolve dns every time you make a request. – Shiplu Mokaddim Jan 25 '12 at 09:16
  • hmm, 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) – antfx Jan 25 '12 at 09:20
  • Anyone found the solution for this? I'm also facing the same problem and this host and ip work around is not working for me. – Sagar Panwala Mar 17 '16 at 06:16
3

I know this is an old post, but was facing the same error, so thought to share the solution.

  1. 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.
  2. 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 :

System.Net.WebException: Error: NameResolutionFailure when Calling WCF Services throwing exception in mono android application

Community
  • 1
  • 1
NG.
  • 5,695
  • 2
  • 19
  • 30
  • Changing the wifi from my crappy work internet to tethering my LTE made this error go away. – locrizak Sep 28 '15 at 17:41
  • Not 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. – Lewis Hamill Nov 30 '18 at 16:10
  • I've also tried retries with slight sleeps and it didn't eliminate the problem – Eric Legault May 02 '19 at 00:45
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)

Kim Rasmussen
  • 453
  • 1
  • 8
  • 21
  • do you have a reproducible sample? – Alex Sorokoletov Oct 24 '15 at 21:15
  • 1
    No. 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). – Kim Rasmussen Oct 26 '15 at 08:11
0

I was experiencing the same issue in my mono application on raspbian. I've tried different solutions described in this and other threads but none worked. Eventually, I was able to fix the problem by changing the name servers in /etc/resolv.conf to the google ones https://developers.google.com/speed/public-dns/

Mirko

0

I was getting this error when I started the mobile app (android or iOS it does not matter) without internet connection. After restored the connection every request returns "NameResolutionFailure exception". I had to wait 120 seconds for having the http request working again. Setting the following line of code anywhere in the app startup the error was finally gone.

System.Net.ServicePointManager.DnsRefreshTimeout = 0;

The default DnsRefreshTimeout value is 120 seconds.

neurona.dev
  • 1,347
  • 1
  • 14
  • 12