Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest NameResolutionFailure exception in .NET (with Mono on Ubuntu)

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?

like image 200
antfx Avatar asked Jan 25 '12 08:01

antfx


3 Answers

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.

like image 54
Shiplu Mokaddim Avatar answered Nov 19 '22 00:11

Shiplu Mokaddim


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

like image 42
NG. Avatar answered Nov 18 '22 23:11

NG.


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)

like image 3
Kim Rasmussen Avatar answered Nov 18 '22 23:11

Kim Rasmussen