HTTP Proxy Error Detection for WebRequest

How to detect that WebRequest crashes due to a web proxy error and not a target web server error?

try
{
    var request = (HttpWebRequest)WebRequest.Create("http://www.example.com");
    request.Proxy = new WebProxy("localhost");
    var response = request.GetResponse();

    return response.GetResponseStream();
}
catch(WebException webex)
{
    //Detect proxy failure
}
+5
source share
2 answers

It's complicated. Here are some suggestions:

  • The property webex.Response.ResponseUricontains the URI of your proxy server instead of the server you were trying to contact.
  • A property webex.Response.StatusCodeis one that always refers to a proxy problem, for example. ProxyAuthenticationRequired. Unfortunately, most statuses can refer to either a proxy error or a server error.
  • webex.Response.Headers , -. , - Squid "X-Squid-Error" .
  • webex.Response.ResponseStream HTML , -. , URI -.

catch , WebException, , . .

+3

, InvalidOperationException, "".

:

: "localhost"

+1

All Articles