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

前端 未结 5 1391
我寻月下人不归
我寻月下人不归 2021-01-01 20:14

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:

相关标签:
5条回答
  • 2021-01-01 20:35

    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.

    0 讨论(0)
  • 2021-01-01 20:38

    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)

    0 讨论(0)
  • 2021-01-01 20:39

    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.

    0 讨论(0)
  • 2021-01-01 20:45

    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 讨论(0)
  • 2021-01-01 20:53

    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

    0 讨论(0)
提交回复
热议问题