Why does a bad url result in UnknownError, instead of a 400 or 404 code? WebException

830 Views Asked by At

Why does this not create WebExceptionStatus.ProtocolError? Instead I get WebExceptionStatus.UnknownError.

string url = "http://www.ogggle.com/"; // a bad url

var request = (HttpWebRequest) WebRequest.Create(url);

...options setup ellided

using (var response = (HttpWebResponse) request.GetResponse())
    {
        foreach (Cookie cookie in response.Cookies)
            cookies.Add(cookie);

        string urlContent = "";
        using (var sr = new StreamReader(response.GetResponseStream()))
            urlContent = sr.ReadToEnd();

        return urlContent;
    }

which will now throw a WebException; however WebExceptionStatus is actually UnknownError. I am expecting a ProtocolError.

catch (WebException we)
   {
    string message = "WEB EXCEPTION DETECTED. Retry counter: " + _retryCount;
    message += Environment.NewLine + "Problem with url: " + url + ". Status is: " + we.Status;
    message += Environment.NewLine + "Message is: " + we.Message;

    if (we.Status == WebExceptionStatus.ProtocolError)
    {
        message += Environment.NewLine + string.Format("Status Code : {0}", ((HttpWebResponse)we.Response).StatusCode);
        message += Environment.NewLine + string.Format("Status Description : {0}", ((HttpWebResponse)we.Response).StatusDescription);
    }

    WriteToErrorLog(message);
    if (DEBUG)
        Console.WriteLine(message);
    _retryCount++;
   }

From my perspective, the WebException should be smarter than this. A malformed or bad url should result in a meaningful WebExceptionStatus. Instead it results in the less-than-helpful UnknownError. Can anyone help me to understand how to get a meaningful WebException when the the url is a bad one?

1

There are 1 best solutions below

3
On

You do not provide enough information in your question to figure out what the problem is. What is the real "bad" URL that you are having a problem with? WebRequest.Create(string) calls new Url(string), which will throw a UriFormatException exception when the given string is not a valid URL. That's what it does with your given code and the string .