0

We faced a strange issue. Until yesterday and for many years we have in use a small piece of code in our application to access a specific URL in order to check if a specific file exist inside:

    public static bool IsUpdateAvailable ()
    {
        System.Net.WebRequest webRequest = System.Net.WebRequest.Create("http://site/updatefile.exe");
        System.Net.WebResponse webResponse;

        try
        {
            webResponse = webRequest.GetResponse();
        }
        catch (System.Net.WebException e) //If WebException exception thrown then couldn't get response from address
        {
            Console.WriteLine("This program is throw a WebException."+
                        "\n\nException Message :" + e.Message);
              if(e.Status == System.Net.WebExceptionStatus.ProtocolError) return false;
        }
        catch (Exception e) //If general exception thrown then couldn't get response from address
        {
            return false;
        }

        return true;
    }

From yesterday the above code stops to return a 404 error if the checked file or the URL did not exist and thus always return true. We cannot explain from a c# view what happen. Any help will be appreciated.

Nikolas
  • 45
  • 6

1 Answers1

1

Catch WebException and from it you'll be able to recover WebResponse Response, cast it into HttpWebResponse. There you'll get a StatusCode you expect.