在 .Net 2.0 中,以下内容曾经有效:
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://www.google.com");
WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
我使用的是 .Net 4.5,现在它返回 IWebProxy
而不是 WebProxy
。
如何将其转换为 WebProxy
而不是 IWebProxy
?
我想要执行此操作的原因是检查 proxy.Address.AbsoluteUri
并且无法使用 IWebProxy
访问它。
最佳答案
看起来WebRequest.DefaultWebProxy
总是返回一个IWebProxy
,但是底层的具体类型已经改变了。因此,与其依赖底层类型,不如尽可能确定如何通过接口(interface)执行相同的操作。为此,以下内容应该为您提供所需的 Uri
。
WebRequest.DefaultWebProxy.GetProxy(httpWReq.Address).AbsoluteUri
关于c# - System.InvalidCastException : Unable to cast object of type 'WebProxyWrapper' to type 'System.Net.WebProxy' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32509742/