private static bool IsProxyValid(IWebProxy proxy, Uri uri)
{
bool result = true;
WebRequest request = CreateRequest(uri);
WebResponse response = null;
// if we get a null proxy from the caller then don't use it and just re-set the same proxy that we
// already have because I am seeing a strange performance hit when a new instance of a proxy is set
// and it can take a few seconds to be changed before the method call continues.
request.Proxy = proxy ?? request.Proxy;
try {
// During testing we have observed that some proxy setups will return a 200/OK response
// even though the subsequent calls are not going to be valid for the same proxy
// and the set of credentials thus giving the user a 407 error.
// However having to cache the proxy when this service first get's a call and using
// a cached proxy instance seemed to resolve this issue.
response = request.GetResponse();
}
catch (WebException webException) {
HttpWebResponse webResponse = webException.Response as HttpWebResponse;
if (null == webResponse || webResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired) {
result = false;
}
}
finally {
if (null != response) {
((IDisposable)response).Dispose();
}
}
return result;
}