[英]Detect HTTP Proxy error for WebRequest
How to detect that a WebRequest failed due to a web proxy error and not a target web server error? 如何检测WebRequest由于Web代理错误而不是目标Web服务器错误而失败?
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
}
It is difficult. 这很难。 Here are some suggestions: 以下是一些建议:
webex.Response.ResponseUri
property contains the URI of your proxy server instead of the server you were trying to contact. webex.Response.ResponseUri
属性包含代理服务器的URI,而不是您尝试联系的服务器的URI。 webex.Response.StatusCode
property is one that always refers to a proxy problem, eg ProxyAuthenticationRequired
. webex.Response.StatusCode
属性总是引用代理问题,例如ProxyAuthenticationRequired
。 Unfortunately most statuses could refer to either a proxy error or a server error. 不幸的是,大多数状态可能是指代理错误或服务器错误。 webex.Response.Headers
collection contains non-standard entries that you recognise as being generated by your proxy server. webex.Response.Headers
集合包含您认为由代理服务器生成的非标准条目。 For example, the Squid proxy returns the header "X-Squid-Error"
, with its own proprietary set of statuses. 例如,Squid代理返回标题"X-Squid-Error"
,具有自己的一组状态。 webex.Response.ResponseStream
stream contains an HTML or plain text error message in a format that you recognise as being generated by your proxy server. webex.Response.ResponseStream
流包含HTML或纯文本错误消息,其格式为您认为由代理服务器生成的格式。 You might test to see if it contains the URI of your proxy server. 您可以测试它是否包含代理服务器的URI。 In your catch block, make sure that you log full details of the WebException
object, including all the properties mentioned above. 在catch块中,确保记录WebException
对象的完整详细信息,包括上面提到的所有属性。 You can then analyse the log data and develop an accurate test for proxy errors. 然后,您可以分析日志数据并开发准确的代理错误测试。
I think you could catch InvalidOperationException and then check the message for "proxy". 我认为您可以捕获InvalidOperationException然后检查消息“proxy”。
The message would say: 消息会说:
The proxy name could not be resolved: 'localhost' 无法解析代理名称:'localhost'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.