c# - System.InvalidCastException : Unable to cast object of type 'WebProxyWrapper' to type 'System.Net.WebProxy'

标签 c# system.net.httpwebrequest

在 .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/

相关文章:

c# - 文本框 MVVM 数据绑定(bind)

c# - 捕获的变量 IndexOutOfBounds

c# - 建立 SSL\TLS 连接 (X509Chain.Build()) 花费的时间太长

c# - HttpWebRequest 强制新连接

c# - HttpWebRequest 返回错误 "The underlying connection was closed"

c# - TFS 2010 - 为什么我在模拟时收到 "TF30063 You are not authorized to access.."错误?

c# - 使用 knockout.js 渲染部分 View

c# - 在一个 View 中返回多个 View

asp.net - CookieContainer 和 Response.Cookies 集合有什么区别?

.net - 使用 GoogleApisClient ServiceAccountCredential 调用 RequestAccessTokenAsync 抛出异常