locked
System.Net.WebException Error: NameResolutionFailure RRS feed

  • Question

  • User7640 posted

    I periodically get this exception thrown from an android app I am writing side by side with an iOS application. At first, I thought it was a problem with my service being down but, I no longer think this is the case (I never un into the exception in iOS and it is running the EXACT same code). It may or may not matter, but this is running on a Nexus 4 with Android 4.3.

    Any ideas as to why this error would happen, and why it would be random?

    The stack trace looks like this...

    {System.Net.WebException: Error: NameResolutionFailure at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/b76e8ec4/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:945 at IMI.Webstore.Mobile.Core.RestClient.WebRequestExtensions.GetResponse (System.Net.WebRequest request) [0x00033] in c:\Users\jkrebs\code\3Wire\WebStore-Main\WebStoreMobile\Common\IMI.Webstore.Mobile.Core\RestClient\WebRequestExtensions.cs:23 at IMI.Webstore.Mobile.Core.RestClient.RestClient.MakeRequestSynchronous[Part] (System.String requestUrl, System.String verb, System.Object bodyContent) [0x00071] in c:\Users\jkrebs\code\3Wire\WebStore-Main\WebStoreMobile\Common\IMI.Webstore.Mobile.Core\RestClient\RestClient.cs:107 }

    Friday, August 23, 2013 3:27 PM

All replies

  • User209 posted

    Did you give your app the correct permissions?

    Friday, August 23, 2013 3:38 PM
  • User7640 posted

    Yeap I did, Most requests work just fine, just periodically, only on Android 4.3, I get that exception... Any other ideas?

    Monday, August 26, 2013 6:40 PM
  • User209 posted

    Not sure at all. I doubt using HttpClient will change anything, but maybe worth a try.

    Monday, August 26, 2013 9:05 PM
  • User19386 posted

    If Joshua Krebs is in the same situation than me, he uses the Portable Class Library (PCL) to write common code between both iOS and Android version. In that case, the HttpClient is not supported by PCL.

    It's pretty much my problem as well, I get this NameResolutionFailure intermittently on a local server in my company. I also already added the proper permissions.

    Any suggestions? or anyone else with that problem?

    Wednesday, September 4, 2013 2:33 PM
  • User7640 posted

    @ForgeMagic this is indeed the same situation I am in... :(

    Wednesday, September 4, 2013 4:17 PM
  • User1669 posted

    So you're running on a Nexus 4 with Android 4.3? Does this happen with WiFi only, or does it happen over cellular too? The Nexus 4 is know to have issues with WiFi that was introduced with Android 4.3. Perhaps the wifi is just flaking out. Have you tried on another device, or maybe downgrade your device to Android 4.2?

    https://code.google.com/p/android/issues/detail?id=58230

    Wednesday, September 4, 2013 5:24 PM
  • User19386 posted

    @rmacias I forgot to mention, in my case I am using a Nexus 7 on Android 4.3 as well. Kernel version 3.1.10-g1e8b3d8.

    I am on the Wifi, but since it's intermittent the same call will pass on the first execution and can fail later on when called again.

    EDIT: Although by reading through more of the post you have sent, it seems that the Nexus 7 also holds the problem. Hopefully that will be fixed soon. This is really annoying.

    Wednesday, September 4, 2013 7:40 PM
  • User7640 posted

    Not to post on an older thread (I've been ignoring this issue for a bit now) I am still having the issue occasionally on the nexus 4 but haven't yet been able to isolate it to Wifi, I do know for certain though, that pre-upgrade it worked perfectly which would lead me to believe it could be the wifi issue. I do MOST of my dev over wifi maybe I will try over cell network for a while and see if it helps.

    I could easily downgrade my version of android, but I don't have control over the users of the app I'm developing and it would be a shame to say they can't upgrade if they want to use the app.

    Tuesday, September 24, 2013 7:41 PM
  • User24226 posted

    I'm having the same issue on Android 4.2.1 (and it seems this guy does too: https://bugzilla.xamarin.com/show_bug.cgi?id=13750).

    I'll see if another emulator/device makes any difference.

    Does using System.Net.Http.HttpClient fix this?

    Update Turned out to be an issue with our office's network - glad I spent an hour or so chasing that.

    Wednesday, October 2, 2013 4:10 PM
  • User19386 posted

    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.

    In any cases, I show a case per case message to the user.

    Wednesday, October 16, 2013 9:56 PM
  • User4914 posted

    @ForceMagic - I only get it when connected to WiFi too. My office is at the outer edge of WiFi coverage so my phone has an unstable connection at times.

    Retrying doesn't seem to be a good option for me. I've tried programmatically switching WiFi off and on, but that's too slow and is probably way out of bounds in terms of UX.

    Friday, October 18, 2013 10:57 PM
  • User11701 posted

    Just ran up against this and I can't believe that it worked with mobile network but not with WiFi! Frankly that's just insane!

    Wednesday, November 20, 2013 8:54 PM
  • User29552 posted

    For me Its works in 4.2 or lower. But in 4.3 or 4.4 the first call to rest service using HttpWebRequest is working fine and the subsequent requests are failing. Any solution to overcome this?

    Tuesday, December 10, 2013 4:40 AM
  • User29306 posted

    I'm having the same problem. Nexus 4, over WiFi. I'm using HttpClient. It also fails on the emulator. It seems erratic in when it fails.

    Thursday, December 26, 2013 8:44 PM
  • User31313 posted

    Solve this using IP instead url. Try this.

    Tuesday, January 7, 2014 3:30 PM
  • User41104 posted

    I also got this strange exception when downgrading from Android 4.1 to 2.3 and moving the solution from Debug to Release. Strange.

    Tuesday, March 4, 2014 7:46 PM
  • User209 posted

    @ForceMagic? HttpClient is supported in PCL just grab the HttpClient from NuGet.

    EDIT: Oh nvm, someone vamped a thread again...

    Tuesday, March 4, 2014 11:49 PM
  • User19386 posted

    @Cheesebaron? Is this new since the last release, because I remember almost a year ago, we had to use low level request because of PCL.

    Wednesday, March 5, 2014 12:42 PM
  • User209 posted

    It is new since PCL was officially released.

    Wednesday, March 5, 2014 12:50 PM
  • User2448 posted

    Get same problem on Galaxy s4 after upgrade to Android 4.3 and 4.4. And obviously only when connecting via WiFi

    Wednesday, March 5, 2014 4:32 PM
  • User19386 posted

    @Cheesebaron? Ok, thanks. We will see if we can use that instead in the future, for now we'll stick with the low level Request since the system is done anyway.

    Wednesday, March 5, 2014 6:11 PM
  • User41104 posted

    We have this NameResolutionFailure problem on the emulator after first upgrading the Xamarin. Did you have a similar problem? In our case, the use of IP does not work, indicating that the emulator does not have access to the net.

    Tuesday, March 11, 2014 8:05 AM
  • User41104 posted

    And we solved it by setting permission app:Internet

    Tuesday, March 11, 2014 9:41 AM
  • User49477 posted

    Anyone got a solution?

    Friday, April 25, 2014 6:54 PM
  • User49775 posted

    Hello! I get this problem if i havent restarted the emulator for a while. Ive never had it reoccur right after a restart of emulator and Xamarin. Seems like my emulator gets confused after a while.

    Wednesday, May 14, 2014 4:10 PM
  • User44048 posted

    I get this on my problem on my cell phone (Android Note 3) if I'm connected to the Wifi. Any wise words about this would be appreciated.

    Thursday, May 15, 2014 4:45 PM
  • User52861 posted

    Someone has found a solution?

    Tuesday, May 20, 2014 2:49 AM
  • User49009 posted

    I also have this strange when you are downgrading from Android 4.1 to 2.3 and moving solution to debug to release.

    Tuesday, May 20, 2014 9:58 AM
  • User51083 posted

    I had the same problem. I found that Data Plan on the Device was disabled some reason. In Galaxy S4->Settings->Data Usage-> Mobile Data , Check the check box.that should fix the issue.

    Thursday, May 22, 2014 9:34 PM
  • User52352 posted

    You must use the IP address from which you invoke:

    rxcui var = "1";

    WebRequest request = HttpWebRequest.Create(string.Format(@"http://xxx.xxx.xxx.xxx/ServicesRest/api/Demostration/GetEntityPerson?id={0}", rxcui));

    Friday, May 23, 2014 1:02 PM
  • User52352 posted

    If you use a wifi with proxy

    Friday, May 23, 2014 1:02 PM
  • User28549 posted

    Deleted by author

    Friday, May 23, 2014 7:04 PM
  • User61482 posted

    We ran into the same issue, by reading one of the posts here found that wifi was the issue. This was fixed by using the LAN connection

    Friday, August 8, 2014 3:06 PM
  • User1549 posted

    I'm hitting this with a brand new Galaxy S5 with the latest firmeware as of today (android 4.4.4). This is a dev phone and has no SIM card in it, only wifi...this is crazy.

    Any real solutions this? My app has the correct permissions and adding retries seems to work, but that shouldn't be needed.

    Wednesday, January 14, 2015 10:16 PM
  • User209 posted

    I still sometimes get this. However, if I keep creating requests using the same instance of HttpClient the exception eventually go away.

    So you could try something like Polly or create your own Retry Handler. It is not pretty, but I remember reading about people having troubles with some devices when writing Java as well, and what helps is to toggle the WiFi connection.

    Wednesday, January 14, 2015 10:38 PM
  • User1549 posted

    @Cheesebaron? do you know if it happens with OkHttp (via ModernHttpClient) too or if that'd fix it?

    Wednesday, January 14, 2015 11:51 PM
  • User209 posted

    @OrenNovotny? yes, happens with that as well.

    Thursday, January 15, 2015 12:54 AM
  • User93549 posted

    @OrenNovotny I'm running into the same issue... (with the same device setup) did you figure out the issue?

    Monday, February 23, 2015 5:12 PM
  • User1549 posted

    No solution - just wound up using the Polly library to put a retry around it. It just seems flaky but retries work.

    Monday, February 23, 2015 5:14 PM
  • User100319 posted

    same problem in release version of android 4.4

    Thursday, March 5, 2015 2:44 AM
  • User2148 posted

    I've the same problem, but with SIM. with WIFI it works

    Tuesday, April 21, 2015 9:18 PM
  • User114278 posted

    For me, in release mode, I checked the box marked Internet in the Required permissions section of the AndroidManifest.xml file found under the Properties folder. Debug mode doesn't need this checked but Release mode does - which you need if sending the APK by email for installation - which works great by the way. In case anyone is interested, you also have to sign your app and produce the private key following these instructions here: http://developer.xamarin.com/guides/android/deployment,testing,andmetrics/publishinganapplication/part2-publishinganapplicationongoogle_play/

    Wednesday, April 29, 2015 12:53 PM
  • User74871 posted

    Same problem when the device has a limited connectivity with the server, a timeout or airplane mode. I can catch the exception but the next instruction generates a "FATAL EXCEPTION: main".

    Any solution?

    Monday, June 29, 2015 1:54 PM
  • User74871 posted

    It seems that the root of the problem was because I prepended ".ConfigureAwait(false)" to "await" instructions. I removed it to the next awaitable instruction and now everything works as expected.

    Tuesday, June 30, 2015 12:58 PM
  • User209 posted

    I highly doubt the issue is .ConfigureAwait(false). The only thing it does is to not switch to the context we were on before calling await on the given Task when it returns. That has no implications on whether or not you will get a NameResolutionFailure.

    Tuesday, June 30, 2015 1:09 PM
  • User74871 posted

    Hi Tomasz,

    You are true, .ConfigureAwait(false) only throw an exception on the next awaitable instruction. There is no link with NameResolutionFailure which is caught in a try/catch.

    Example with failure: await Test1Async().ConfigureAwait(false); await Test2Async().ConfigureAwait(false); // => Crash

    Example with success: await Test1Async(); await Test2Async(); // => Succeeded

    In the failure scenario there is no way to catch the exception wity a try/catch, the application directly crash. According to my tests that only happens when doing HTTP calls and when the user has a limited connectivity (e.g. airplane mode throwing NameResolutionFailure), something seems to break in Mono.

    I also saw on a forum that there was an issue in Xamarin Insights and the problem was because there was no try/catch in Mono for some HTTP calls. It does not seem to be fixed yet unless you manually upgrade Mono. This problem may be related.

    Thursday, July 2, 2015 8:19 AM
  • User209 posted

    @Gilles.5055 That is true if your two methods Test1Async and Test2Async are void! If you return Task instead, the exception will bubble up.

    private async Task Test1Async() { ... }

    Thursday, July 2, 2015 8:54 AM
  • User139760 posted

    Noticed none of the solutions suggested here worked for me so I have shared a post for what I did to resolve the problem on the link below. http://forums.xamarin.com/discussion/44929/how-to-solve-the-system-web-webexception-name-resolution-failure?new=1

    Saturday, July 4, 2015 4:57 AM
  • User74871 posted

    @Cheesebaron Yes, all my methods return a Task or Task<T>, they are never a void.

    Monday, July 6, 2015 7:39 AM
  • User137947 posted

    I have the same error but on iOS device, on wifi it works fine but on cellular 3G I get this exceptions

    Thursday, August 27, 2015 12:22 PM
  • User162947 posted

    I use the HttpClient in a PCL and had the same problem on an Android device (worked fine on a Windows Phone).

    I got it working when I added the Host to the header: client.DefaultRequestHeaders.Host = "mydomain.com";

    Thursday, October 1, 2015 7:15 PM
  • User19867 posted

    Hey I am finding the same error on iOS and Android when using wifi + httpclient but only randomly. I can't use an ip to solve the problem because our servers have a load balancer and rotating IP's. Has anyone tried the RestSharp component and seen any different results?

    Thursday, November 12, 2015 4:06 PM
  • User173263 posted

    It's pretty much my problem as well, I get this Name Resolution Failure intermittently on a local server in my company. I also already added the proper permissions.

    Friday, November 13, 2015 9:22 AM
  • User182151 posted

    How we connect sql server into the Xamarin using visual studio

    Thursday, January 14, 2016 11:21 AM
  • User170762 posted

    I had the same issue. After rumbling with the error I found out my mobile wifi was not stable. I tried on my other samsung galaxy s6 plus and it worked fine. Also make sure you have ticked the INTERNET Permission. For reference the code below works for me.

    var client = new System.Net.Http.HttpClient(); System.Net.Http.HttpResponseMessage response1 = await client.GetAsync(@"http://192.168.1.11/RestServiceApplication/Service1.svc/GetFirstName"); var placesJson = response1.Content.ReadAsStringAsync().Result; var content = JsonConvert.DeserializeObject<string>(placesJson);

    Monday, January 25, 2016 3:18 AM
  • User75709 posted

    Anyone found the solution for this? Can any one from @XamarinCompany will respond to this?

    Thursday, March 17, 2016 6:26 AM
  • User142160 posted

    HI,

    According to me...

    When you use a .NET client to consume a Web service through an HTTP proxy server, you may receive the following error message. you need to set your server setting and enable as proxy. Please refer to https://support.microsoft.com/en-us/kb/318140 for your server setting.

    Thanks..!!

    Friday, March 18, 2016 10:59 AM
  • User201613 posted

    Same Problem please help

    string url = "test.mysite.com/test.php"; // System.Net.WebException: Error: NameResolutionFailure
    // but url = "ipaddress/test.php"; works just fine
    // I wanna be able to use a domain name since the ip will just give me www.mysite.com and not test.mysite.com
    // Im using xamarin forms project from visual studio 2015 updated on june 9 2016
    string test= "test=342";
    await MakePostRequest(url, test, false);    
    
    public async Task<string> MakePostRequest(string url, string serializedDataString, bool isJson = true) {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
                if (isJson)
                    request.ContentType = "application/json";
                else
                    request.ContentType = "application/x-www-form-urlencoded";
    
                request.Method = "POST";
    
                var stream = await request.GetRequestStreamAsync();
                using (var writer = new StreamWriter(stream)) {
                    writer.Write(serializedDataString);
                    writer.Flush();
                    writer.Dispose();
                }
    
                var response = await request.GetResponseAsync();
                var respStream = response.GetResponseStream();
    
                using (StreamReader sr = new StreamReader(respStream)) {
                    return sr.ReadToEnd();
                }
            }
    
    Thursday, June 9, 2016 4:53 PM
  • User234467 posted

    The error seems to be occurring when I create multiple instance of HttpClient. What worked for me was to use HttpClient in a singleton pattern. And then called it wherever I needed throughout my application.

    public sealed class HttpClientInstance { private static readonly HttpClient HttpClient;
    static HttpClientInstance() { HttpClient = new HttpClient(); } public static HttpClient Instance { get { return HttpClient; } } }

    This is how I called it throughout my application. ... HttpClient client = HttpClientInstance.Instance; ...

    Wednesday, July 13, 2016 1:39 PM
  • User25553 posted

    I know how stupidly simple this sounds, but try setting the emulator's network to cellular data instead of WiFi.

    I recently started seeing the NameResoutionFailure errors in the Visual Studio Emulator for Android while working at a hotel. And then at a conference later the same day, the app ran fine; no NameResolutionFailure messages.

    And then the next day, I was on the Microsoft campus and was seeing the NameResolutionFailure errors return. After much searching for solutions and gnashing of teeth, I found this SO answer: http://stackoverflow.com/questions/27740296/the-webview-running-in-the-emulator-in-android-studio-cannot-access-internet-bu#answer-37449935

    To my surprise, as soon as I enabled cell data in the emulator instead of assuming that WiFi would just work, the NameResolutionErrors disappeared and my app was able send and receive data just fine.

    Since I'm using the Visual Studio Emulator for Android, I didn't enable cell data in the operating system itself (as in the SO answer), but in the emulator settings panel. The Visual Studio Emulator for Android has a radio button in its settings that allows you to select WiFi or Cellular, under the "Network" tab.

    As has been mentioned before, the root of the problem is probably some kind of proxy in the network that your host computer is attached to. As to why switching to Cellular instead of WiFi in the emulator resolves it...I have no idea. But it seems to have solved it for me.

    Tuesday, July 26, 2016 6:59 PM
  • User235575 posted

    I had the same issue just adding

    fix the issue for me.

    Wednesday, August 10, 2016 4:32 PM
  • User235575 posted

    I had the same issue but adding the 'uses-permission android:name="android.permission.INTERNET" ' solved the WebException issue.

    Wednesday, August 10, 2016 4:33 PM
  • User242158 posted

    I resoved it! on your device, your wifi is off!

    Wednesday, August 10, 2016 8:21 PM
  • User149443 posted

    Hi,

    I found solution for that the device should be online when working with webapi/Wcf service.

    Regards, Ningappa

    Friday, August 26, 2016 11:48 AM
  • User253061 posted

    If you set any special character in the cookie (like http://google.com), you may get this error. You need to do URL encode and before setting any value to the cookie

    Thursday, September 1, 2016 10:50 PM
  • User149548 posted

    Unhandled managed exception: Error: NameResolutionFailure (System.Net.WebException)

    How do you handle this exception and enable offline mode?

    I'm using Azure App Mobile Service client and server for Xamarin.iOS...

    I get this when the app is in airplane mode

    Monday, September 5, 2016 9:36 PM
  • User221380 posted

    There seems to be a bug in Xamarin Cycle 7. Stumbled across the same issue and fixed it by calling the full domain url of the web api service instead of the IP.

    http://stackoverflow.com/a/37886792/1028868

    Wednesday, September 7, 2016 8:08 AM
  • User117860 posted

    I also faced lot of issues ( NameResolutionfailure) using httpClient, but later when I used WebClient I haven't faced issue till then.

    Thursday, September 8, 2016 11:03 AM
  • User251283 posted

    Simple add uses-permission android:name="android.permission.INTERNET" to the manifest android project

    Friday, October 28, 2016 12:25 PM
  • User267721 posted

    @Eauditor said: And we solved it by setting permission app:Internet

    Thank you @Eauditor I finally solved by setting permission INTERNET. Thanks.

    Thursday, November 3, 2016 9:19 AM
  • User213329 posted

    In Android manifest enabling the 'Internet' permission fixed this for me. Thanks to @Eauditor for the solution.

    Thursday, December 8, 2016 2:24 PM
  • User221466 posted

    Just for reference, @Eautitor solution solved it for me on a Galaxy S4 running Android 5.1. Many thanks!

    Wednesday, January 11, 2017 11:58 AM
  • User45441 posted

    My problem solved by this code:

    var client = new HttpClient { BaseAddress = new Uri("http://1.2.3.4"), DefaultRequestHeaders = { Host = "example.com" } };

    Monday, January 23, 2017 5:42 AM
  • User287127 posted

    have the same issue , I believe this issue because of your internet connect , it may be show it connect but in fact its not

    Monday, February 6, 2017 2:15 PM
  • User297805 posted

    And I also have this issue

    Monday, March 6, 2017 8:20 PM
  • User297805 posted

    I have found a solution that works for me: instead of resolving the local IIS name, I am using the local IP Now I can work with both Azure and local IIS :-)

    Wednesday, March 8, 2017 7:53 AM
  • User279438 posted

    In my case IP was not mapped to particular Url. Beforehand i was using http://abcd48:8080/dashboard Now i am using http://abcd48.example.com:8080/dashboard for testing

    Monday, March 27, 2017 9:54 AM
  • User322110 posted

    Any Solution

    Tuesday, June 20, 2017 8:28 PM
  • User330529 posted

    @DesOwen.5671 said: For me, in release mode, I checked the box marked Internet in the Required permissions section of the AndroidManifest.xml file found under the Properties folder. Debug mode doesn't need this checked but Release mode does - which you need if sending the APK by email for installation - which works great by the way. In case anyone is interested, you also have to sign your app and produce the private key following these instructions here: http://developer.xamarin.com/guides/android/deployment,testing,andmetrics/publishinganapplication/part2-publishinganapplicationongoogle_play/

    You saved my life. :) thanks.

    Friday, July 14, 2017 10:45 AM
  • User261729 posted

    Solved by changing network....

    Thursday, July 27, 2017 8:06 AM
  • User115850 posted

    I'm having this issue ? Do we have any resolution for this ? I tried to access a service from Xamarin.Android application both on real device ( API 19 ) and emulator ( Nexus S API 23 - x86 ).

    In both the cases , i'm getting the same "Name Resolution Error." Any help is appreciated ?

    Wednesday, August 2, 2017 6:46 AM
  • User98713 posted

    I have this issue as well when I build in Release Mode on multiple WLAN's all without a proxy. In Debug it works fine. Also I have set the Internet Permission. Linking is set to None. Any Solution here?

    Tuesday, September 5, 2017 8:05 AM
  • User59747 posted

    could you please provide solution how to handle in client side using code

    Friday, November 10, 2017 11:22 AM
  • User353791 posted

    I solved it also by setting permission for INTERNET within the Android-Manifest

    Wednesday, November 22, 2017 7:22 PM
  • User363692 posted

    Go to Properties -> Android Build -> General HttpClient Implementation - AndroidClientHandler SSL/TLS implementation - Native TLS 1.2+ Working for me.

    Thanks

    Wednesday, January 3, 2018 12:50 PM
  • User370646 posted

    Just switch to orio 8.x emulator it worked for me.

    Tuesday, July 10, 2018 10:00 AM
  • User378529 posted

    I have the same Issue. I realize that the error becomes when i left the emulator opened and change the wifi connection. Close the emulator and re run the app again and the error will solved.

    Thursday, October 11, 2018 6:08 AM