2

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.

juharr
  • 31,741
  • 4
  • 58
  • 93
sapbucket
  • 6,795
  • 15
  • 57
  • 94
  • What about `proxy.GetProxy(httpWReq.Address).AbsoluteUri`? – juharr Sep 10 '15 at 19:08
  • that worked. Will you post the answer so I can award you? As a one-liner, the answer should be: WebRequest.DefaultWebProxy.GetProxy(httpWReq.Address).AbsoluteUri – sapbucket Sep 11 '15 at 15:55

1 Answers1

2

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 
juharr
  • 31,741
  • 4
  • 58
  • 93
  • This does not always return the proxy. For example if using a windows service with a different account, this will not return the proxy. – Robert Smith Feb 24 '20 at 19:13
  • @RobertSmith good to know before I try to find out if docs are right or not. The documentation says it gets proxy for the logged in user. Keywords Logged In User. – Ken Jun 23 '23 at 20:24