none
C# Application throws “The remote name could not be resolved” exceptions after I change my network RRS feed

  • Question

  • My application throws “The remote name could not be resolved” exceptions after I change my network to corporate network.

    Application is written on C# and using WebClient. WebClient instance is created on timer every 3 seconds and gets data from server using UploadValues.

    Office network is set up such way that we have few WiFi networks (let us call them A, B, C, D). A – Is the corporate VPN network.

    When we run the app, it works well regardless of current network connection. If we change connection between B, C, D app keeps working without problems. If we run app while we are connected to A network, and then switch to B, C or D it still works without problems. But if we start app while we are connect to B,С or D and then switch to A (the corporate VPN one) app throws «The remote name could not be resolved» on every timer tick, and if we switch back to B,C or D, UploadValues starts to work just fine. The only way to make app run properly after switch from B, C, D network to A is to restart the app, (if after restart we switch to B, C, D and back to A app will start crushing again with «The remote name could not be resolved». I tried to access same resource via browser (Chrome, IE 10) and it worked just fine.

    I tried to get IP with Dns.GetHostAddresses and it always returns «No such host is known» in A network, but in B, C or D network works just fine. nslookup command returns «Non-existent domain» in A network but works fine in B, C or D. At the same time, I can access resource via Chrome or IE without a problem regardless of the current network.

    ipconfig /flushdns, ipconfig /registerdns and ipconfig /renew don’t work.

    What is causing this problem? At this point, I am completely lost and I am out of ideas.

    Tuesday, June 17, 2014 2:16 PM

Answers

  • Root of the problem is proxy. It is impossible to reach that service without proxy.  For the unknown reason proxy fails to work when user switches networks.  In order for connection to work you, have to specify Proxy on WebClient initialization like this:

    webClient.Proxy = WebRequest.GetSystemWebProxy();
    • Marked as answer by Sergejs Ru Friday, June 20, 2014 11:06 AM
    Friday, June 20, 2014 11:06 AM

All replies

  • Hi Sergejs,

    The error message indicates the object doesn’t have access to the remote location. I guess there are some configuration problems on the network.

    Before you connect the remote resource, try to check the network. If it is different than the previous network, just build a new webclient object. Code maybe like the below.

      private void timer1_Tick(object sender, EventArgs e)
    
            {
    
                //check network
    
    
    
                //if different than the last one
    
            }
    

    A useful reference. http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 18, 2014 5:12 AM
    Moderator
  • That is what I am already doing, creating new web client object every time.

                while (timerThreadActive)
                {
                    ....
    
                    using (CustomWebClient client = new CustomWebClient())
                    {
                        client.Headers.Add(HttpRequestHeader.Cookie, SessionString);
                        client.Headers.Add(HttpRequestHeader.Accept, "*/*");
                        client.Headers.Add("Origin", globalServiceUrlString);
                        client.Headers.Add("Cache-Control", "no-cache");
                        client.Headers.Add(HttpRequestHeader.UserAgent, userAgent);
                        NameValueCollection formData = new NameValueCollection();
                        formData["json"] = functionJsonText;
    
                        byte[] byteResult = client.UploadValues(callUrl, "POST", formData);
                        result = Encoding.UTF8.GetString(byteResult);
                    }
    
                    ...
    
                    Thread.Sleep(refreshDelay);
                }

    CustomWebClient:

        public class CustomWebClient : WebClient
        {
            private X509Certificate2 certificate;
    
            public CustomWebClient()
            {
                certificate = null;
            }
    
            public CustomWebClient(X509Certificate2 certificate)
            {
                this.certificate = certificate;
            }
    
            protected override WebRequest GetWebRequest(Uri address)
            {
                HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
                (request as HttpWebRequest).KeepAlive = false;
                request.AllowAutoRedirect = false;
                request.Timeout = 15000;
                request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
    
                if (certificate != null)
                {
                    request.ClientCertificates.Add(certificate);
                }
    
                return request;
            }
        }

    Wednesday, June 18, 2014 5:58 AM
  • Hi Sergejs,

    According to your code, I think it’s not code problem. If there is something wrong with code, it won’t work.

    I recommend you check your network. You can post thread on the following forum for support. Please refer to http://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverPN.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 19, 2014 8:20 AM
    Moderator
  • Root of the problem is proxy. It is impossible to reach that service without proxy.  For the unknown reason proxy fails to work when user switches networks.  In order for connection to work you, have to specify Proxy on WebClient initialization like this:

    webClient.Proxy = WebRequest.GetSystemWebProxy();
    • Marked as answer by Sergejs Ru Friday, June 20, 2014 11:06 AM
    Friday, June 20, 2014 11:06 AM