How to fetch the default HTTP proxy

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

How do I determine if IE is configured to use a proxy server? If IE is
configured to use a proxy server, then how do I get a WebProxy object
equivalent to the HTTP proxy server that IE is configured to use?

In the registry, HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet
Settings contains keys that appear to tell me if IE is configured to use a
proxy server, and, if so, the URL of the proxy server. I'm just not sure if
reading the registry is the "correct" way to get these values, or if there
is some .Net function that gets me this information in a more supported way.

TIA,

- Bob
 
Bob said:
Hi all,

How do I determine if IE is configured to use a proxy server? If IE
is configured to use a proxy server, then how do I get a WebProxy
object equivalent to the HTTP proxy server that IE is configured to
use?

In the registry,
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings
contains keys that appear to tell me if IE is configured to use a
proxy server, and, if so, the URL of the proxy server. I'm just not
sure if reading the registry is the "correct" way to get these
values, or if there is some .Net function that gets me this
information in a more supported way.

There's no need to peek into the registry; System.Net.GlobalProxySelection
comes to the rescue ;-)


IWebProxy systemProxy = GlobalProxySelection.Select; // Property, not a
method!

Cheers,
 
That doesn't seem to work for me. The proxy that I get back from

systemProxy = GlobalProxySelection.Select

is "empty" (its Address property is blank). According to the docs, "the
default proxy setting is initialized from the global or application config
file".

- Bob
 
Bob said:
That doesn't seem to work for me. The proxy that I get back from

systemProxy = GlobalProxySelection.Select

is "empty" (its Address property is blank). According to the docs,
"the default proxy setting is initialized from the global or
application config file".

Sorry, I got confused. If do not set a specific proxy and your
machine.config specifies

<defaultProxy>
<proxy usesystemdefault="true"/>
</defaultProxy>

(that's the default configuration), HttpWebRequest will use the operating
system's proxy setting. Unfortunately, GlobalPrxySelection.Select does *not*
return an appropriate IWebProxy in this case. So the answer is
a) don't do anything in order to use the system's default proxy, assuming
you're running the default configuration;
b) unfortunately, there seems to be no "easy" API that retrieves it as in
IWebProxy instance.

Cheers,
 
Sorry, you lost me there... What do you mean "don't do anything in order to
use the system's default proxy, assuming you're running the default
configuration"? My computer is set up to use a proxy server. However, if I
don't go out of my way to feed the URL of my proxy server to the WebRequest,
then it fails when it tries to contact the remote website.

- Bob
 
Bob said:
Sorry, you lost me there... What do you mean "don't do anything in
order to use the system's default proxy, assuming you're running the
default configuration"?

Assuming you're running the default .NET configuration (i.e. you didn't
modify the proxy settings in machine.config), you don't need to assign an
IWebProxy instance to your HttpWebRequest's proxy property.
My computer is set up to use a proxy server.
However, if I don't go out of my way to feed the URL of my proxy
server to the WebRequest, then it fails when it tries to contact the
remote website.

Works for me. Does your proxy use authentication?

Cheers,
 
Does your proxy use authentication?

I don't know... how would I tell? In order to get through my corporate
firewall, all I need to do is specify the proxy address and port. I don't
see anything about "authentication" on the dialogs that I get to by going to
Internet Properties, Connections tab, LAN Settings button, Advanced button.

Just to make sure we're on the same page, here is the code I'm using:

Imports System.Net

' This is required to get through my corporate proxy server
Dim proxy As New WebProxy(http://my-proxy.xyz.com:80)
GlobalProxySelection.Select = proxy

Dim url As String = "http://www.somewhere.com/main.html"
dim wr as HttpWebRequest = DirectCast(WebRequest.Create(url),
HttpWebRequest)
With wr
' Set credentials to use for this request
.Credentials = CredentialCache.DefaultCredentials

' Get the response from the website
Dim response As HttpWebResponse = DirectCast(.GetResponse(),
HttpWebResponse)

... etc...
End With
 
Hi Bob,

I agree with Joerg's suggestion.
<proxy> Element
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/gngrfproxyelement.asp

The <proxy> element defines a proxy server for an application. When the
usesystemdefault attribute is true, the application uses the proxy defined
in the Internet Options dialog box.

If we define the usesystemdefault in the proxy element we do not need to
specified the proxy in our code. That is to say we can use the
httpwebrequest just as we access the website directly.

If we use a authentication proxy in internet explorer, usually a dialog
will be pop up which ask for authentication information used to access the
proxy.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hmmm... curious. My machine.config file has not been modified, and it
contains

<defaultProxy>
<proxy usesystemdefault="true"/>

And yet my application doesn't work unless I create a WebProxy object and
provide it as the value of the GlobalProxySelection.Select property.

Do I need to do something to tell .Net to use the settings in
machine.config?

- Bob
 
Here are the rules for proxy behavior in System.Net:

1) The appdomain setting overrides the machine.config/app.config settings.
2) The per-request proxy (set using httpwebrequest.Proxy) overrides #1

Do you want to do authentication for proxy ? If so, set the credential on
the proxy, not the webrequest.

proxy.Credentials = new NetworkCredential("proxyuser", "proxypass",
"proxydomain");

--

Thanks

feroze
=============
This posting is offered as-is. It offers no warranties and confers no
rights.
 
Sorry, I'm missing something really fundamental here...

I apparently don't need to do authentication. (The authentication stuff in
my original example was just copied-and-pasted from the example in the MSDN
docs.) If I remove all references to Credentials, my code still works as
before. As near as I can tell, the minimum code that I need to access a
website in code is:

' These two lines are necessary to get through the firewall
Dim proxy As New WebProxy("<My proxy name:8080>")
GlobalProxySelection.Select = proxy

Dim url As String = "<desired website>"
Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url),
HttpWebRequest)
dim response as HttpWebResponse = DirectCast(wr.GetResponse(),
HttpWebResponse)

I just don't want to hard-code my proxy address in my code. But if I remove
the first two code lines, the call to GetResponse fails.

As another attempt to get this to work, I created an app.config file and put
the following into it:

<?xml version="1.0" encoding="utf-8" ?><configuration>
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="true" />
</defaultProxy>
</system.net>
</configuration>

Then I removed the GlobalProxySelection stuff and tried it. Still doesn't
work.
 
Hi Bob,

I think you may try to run your code on another machine without using
WebProxy and set the <proxy usesystemdefault="true" /> in the
machine.config in that test machine to see if the problem persists, this
will help us isolate the problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Peter,

I'm going to give up on this issue for now... If and when I need to
understand what is going on, I'll burn an MSDN telephone incident.

Many thanks to all for helping me get something of a handle on this issue
(even if I never did get it work properly ;-)

- Bob
 
Hi Bob,

Did you try WebProxy.GetDefaultProxy() ? The GetDefaultProxy method reads
the nondynamic proxy settings stored by Internet Explorer and creates a
WebProxy instance with those settings.

--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/


| Hi all,
|
| How do I determine if IE is configured to use a proxy server? If IE is
| configured to use a proxy server, then how do I get a WebProxy object
| equivalent to the HTTP proxy server that IE is configured to use?
|
| In the registry, HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet
| Settings contains keys that appear to tell me if IE is configured to use a
| proxy server, and, if so, the URL of the proxy server. I'm just not sure
if
| reading the registry is the "correct" way to get these values, or if there
| is some .Net function that gets me this information in a more supported
way.
|
| TIA,
|
| - Bob
|
|
 
Ross,

There must be something seriously strange about the way my PC is set up
(although I'll be darned if I can see what it is). WebProxy.GetDefaultProxy
returns an "empty" Proxy (its .Address property is Nothing) identical to the
proxy I get from GlobalProxySelection.Select(). As I said on the other
branch of this conversational thread, I'm giving up for now. If this really
becomes a practical problem (as opposed to an intellectual curiosity, as it
is now), I'll burn an MSDN incident to get it figured out.

BTW, I downloaded the regular expression designer in your signature line.
Whenever I work with regex, I invariably wind up writing a small test
program to fiddle around with the regex syntax and to try to wrap my brain
around the resultant regex objects (matches, captures, etc.). This is just
the tool I've needed (but didn't realize I needed). Thanks!

- Bob
 
Ross said:
Hi Bob,

Did you try WebProxy.GetDefaultProxy() ? The GetDefaultProxy method
reads the nondynamic proxy settings stored by Internet Explorer and
creates a WebProxy instance with those settings.

Ah... that's the method I was missing in my earlier post. Microsoft, please
move this method to GlobalProxySelection, I really don't see why proxy
retrieval should be scattered all over the BCL.

Cheers,
 
Back
Top