sapbucket
sapbucket

Reputation: 7215

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

In .Net 2.0 the following used to work:

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://www.google.com");
            WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;

I'm using .Net 4.5 and now it is returning IWebProxy instead of WebProxy.

How can I cast it to be WebProxy instead of IWebProxy?

The reason that I want to do this is to check proxy.Address.AbsoluteUri and it isn't accessible using IWebProxy.

Upvotes: 2

Views: 3726

Answers (1)

juharr
juharr

Reputation: 32296

It looks like WebRequest.DefaultWebProxy has always returned a IWebProxy, but the underlying concrete type has changed. So instead of relying on the underlying type it's better to determine how to do the same thing via the interface if possible. To that end the following should give you the Uri you want.

WebRequest.DefaultWebProxy.GetProxy(httpWReq.Address).AbsoluteUri 

Upvotes: 2

Related Questions