This message was discovered on microsoft.public.dotnet.framework.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| 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
|
|
|
| |
|
| |
| |
| Joerg Jooss |
Bob Altman wrote: [Original message clipped]
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,
-- Joerg Jooss Click here to reveal e-mail address
|
|
|
| |
|
| |
| |
| Bob Altman |
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
"Joerg Jooss" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Joerg Jooss |
Bob Altman wrote: [Original message clipped]
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,
-- Joerg Jooss Click here to reveal e-mail address
|
|
|
| |
|
| |
| |
| Bob Altman |
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
"Joerg Jooss" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
| |
| |
| Joerg Jooss |
Bob Altman wrote: [Original message clipped]
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.
[Original message clipped]
Works for me. Does your proxy use authentication?
Cheers,
-- Joerg Jooss Click here to reveal e-mail address
|
|
|
| |
|
|
| |
| |
| Bob Altman |
>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
|
|
|
| |
|
| |
| |
| Peter Huang (VIP) |
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.
|
|
|
| |
|
|
| |
| |
| Bob Altman |
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
|
|
|
| |
|
|
| |
| |
| feroze (VIP) |
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.
"Bob Altman" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Bob Altman |
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.
|
|
|
| |
|
| |
| |
| Peter Huang (VIP) |
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.
|
|
|
| |
|
|
| |
| |
| Bob Altman |
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
|
|
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| |
| Ross Donald |
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/
"Bob Altman" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... | 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 Altman |
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 Donald" <Click here to reveal e-mail addressM> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
|
| |
| Joerg Jooss |
Ross Donald wrote: [Original message clipped]
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,
-- Joerg Jooss Click here to reveal e-mail address
|
|
|
| |
|
| |
| |
| Phil Hochstetler |
Actually, if you are willing to do some work, you can use P/Invoke to mimic the way IE does proxy detection.
Here is some code:
public class ProxyUtil { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ProxyUtil));
/// <summary> /// No reason to instance this class. /// </summary> private ProxyUtil() { }
#region WIN32 Constants
const UInt32 WINHTTP_AUTOPROXY_AUTO_DETECT = 0x00000001; const UInt32 WINHTTP_AUTOPROXY_CONFIG_URL = 0x00000002; const UInt32 WINHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001; const UInt32 WINHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002;
const Int32 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY = 0; const Int32 WINHTTP_ACCESS_TYPE_NO_PROXY = 1; const Int32 WINHTTP_ACCESS_TYPE_NAMED_PROXY = 3;
const Int32 ERROR_WINHTTP_AUTODETECTION_FAILED = 12180; const Int32 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR = 12178; const Int32 ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT = 12166; const Int32 ERROR_WINHTTP_INCORRECT_HANDLE_TYPE = 12018; const Int32 ERROR_WINHTTP_INTERNAL_ERROR = 12004; const Int32 ERROR_WINHTTP_LOGIN_FAILURE = 12015; const Int32 ERROR_WINHTTP_OPERATION_CANCELLED = 12017; const Int32 ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT = 12167; const Int32 ERROR_WINHTTP_UNRECOGNIZED_SCHEME = 12006; const Int32 ERROR_NOT_ENOUGH_MEMORY = 8;
#endregion
#region WIN32 Structures
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct WINHTTP_AUTOPROXY_OPTIONS { [MarshalAs(UnmanagedType.U4)] public UInt32 dwFlags; [MarshalAs(UnmanagedType.U4)] public UInt32 dwAutoDetectFlags; public string lpszAutoConfigUrl; public IntPtr lpvReserved; [MarshalAs(UnmanagedType.U4)] public UInt32 dwReserved; [MarshalAs(UnmanagedType.Bool)] public bool fAutoLoginIfChallenged; }
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct WINHTTP_PROXY_INFO { [MarshalAs(UnmanagedType.U4)] public int dwAccessType; public IntPtr lpszProxy; // must free after call public IntPtr lpszProxyBypass; // must free after call }
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct WINHTTP_CURRENT_USER_IE_PROXY_CONFIG { [MarshalAs(UnmanagedType.Bool)] public bool fAutoDetect; public IntPtr lpszAutoConfigUrl; // must free after call public IntPtr lpszProxy; // must free after call public IntPtr lpszProxyBypass; // must free after call
}
#endregion
#region DLLIMPORT
[DllImport("Winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)] private static extern IntPtr WinHttpOpen(String userAgent, UInt32 accessType, String proxyServer, String proxyBypass, UInt32 flags);
[DllImport("Winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)] private static extern Int32 WinHttpGetProxyForUrl( IntPtr session, String url, ref WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions, ref WINHTTP_PROXY_INFO proxyInfo);
[DllImport("Winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)] private static extern Int32 WinHttpCloseHandle(IntPtr hInternet);
[DllImport("Winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)] private static extern Int32 WinHttpGetIEProxyConfigForCurrentUser( ref WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyOptions);
#endregion
/// <summary> /// Set the proxy server object for any web request. /// </summary> /// <param name="result">web request object</param> public static void SetProxyServer(WebServicesClientProtocol result) { if (Frame.Context.WebProxy != null) { result.Proxy = new WebProxy("http://" + Frame.Context.WebProxy); if (Frame.Context.WebProxyAuth) result.Proxy.Credentials = CredentialCache.DefaultCredentials; } }
/// <summary> /// Discover any proxy settings required for web services. /// If the "Automatically Detect Settings" checkbox is turned on, /// we attempt to do the WPAD style detection. Otherwise, look /// at the proxy settings set manually (if any). /// </summary> /// <param name="url">the absolute URI to determine the proxy of</param> /// <returns>a string of the form "hostname:port" if success, else null if no proxy server setting exists</returns> public static string DiscoverProxyServer(string url) { string userAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; string proxyServer = null; WINHTTP_AUTOPROXY_OPTIONS proxyOptions = new WINHTTP_AUTOPROXY_OPTIONS(); WINHTTP_PROXY_INFO proxyInfo = new WINHTTP_PROXY_INFO(); WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyInfo = new WINHTTP_CURRENT_USER_IE_PROXY_CONFIG(); proxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; proxyOptions.dwAutoDetectFlags = (WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A); proxyOptions.lpszAutoConfigUrl = ""; proxyOptions.lpvReserved = IntPtr.Zero; proxyOptions.dwReserved = 0; proxyOptions.fAutoLoginIfChallenged = true; IntPtr handle = IntPtr.Zero; Int32 ret = 0;
try { handle = WinHttpOpen(userAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, null, null, 0); if (handle == IntPtr.Zero) { log.Error("WinHttpOpen Error: " + Marshal.GetLastWin32Error()); return proxyServer; } ret = WinHttpGetIEProxyConfigForCurrentUser(ref ieProxyInfo); if (ret == 0) { log.Error("WinHttpGetIEProxyConfigForCurrentUser Error: " + Marshal.GetLastWin32Error()); return proxyServer; }
// If "Automatically Detect Settings" checkbox is checked, we attempt to use WPAD. if (ieProxyInfo.fAutoDetect) { ret = WinHttpGetProxyForUrl(handle, url, ref proxyOptions, ref proxyInfo); if (ret == 0) { int win32Error = Marshal.GetLastWin32Error(); string errorMsg = win32Error.ToString(); switch (win32Error) { case ERROR_WINHTTP_AUTODETECTION_FAILED: errorMsg = "ERROR_WINHTTP_AUTODETECTION_FAILED"; break; case ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR: errorMsg = "ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR"; break; case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT: errorMsg = "ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT"; break; case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE: errorMsg = "ERROR_WINHTTP_INCORRECT_HANDLE_TYPE"; break; case ERROR_WINHTTP_INTERNAL_ERROR: errorMsg = "ERROR_WINHTTP_INTERNAL_ERROR"; break; case ERROR_WINHTTP_LOGIN_FAILURE: errorMsg = "ERROR_WINHTTP_LOGIN_FAILURE"; break; case ERROR_WINHTTP_OPERATION_CANCELLED: errorMsg = "ERROR_WINHTTP_OPERATION_CANCELLED"; break; case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT: errorMsg = "ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT"; break; case ERROR_WINHTTP_UNRECOGNIZED_SCHEME: errorMsg = "ERROR_WINHTTP_UNRECOGNIZED_SCHEME"; break; case ERROR_NOT_ENOUGH_MEMORY: errorMsg = "ERROR_NOT_ENOUGH_MEMORY"; break; } log.Error("WinHttpGetProxyForUrl Error: " + errorMsg); return proxyServer; } if (proxyInfo.lpszProxy != IntPtr.Zero) proxyServer = Marshal.PtrToStringUni(proxyInfo.lpszProxy); log.Info("WinHttpGetProxyForUrl Proxy Server Settings: "); string accessTypeMsg = proxyInfo.dwAccessType.ToString(); switch (proxyInfo.dwAccessType) { case WINHTTP_ACCESS_TYPE_DEFAULT_PROXY: accessTypeMsg = "WINHTTP_ACCESS_TYPE_DEFAULT_PROXY"; break; case WINHTTP_ACCESS_TYPE_NO_PROXY: accessTypeMsg = "WINHTTP_ACCESS_TYPE_NO_PROXY"; break; case WINHTTP_ACCESS_TYPE_NAMED_PROXY: accessTypeMsg = "WINHTTP_ACCESS_TYPE_NAMED_PROXY"; break; } log.Info(" AccessType: " + accessTypeMsg); if (proxyInfo.lpszProxy != IntPtr.Zero) log.Info(" Proxy: " + Marshal.PtrToStringUni(proxyInfo.lpszProxy)); if (proxyInfo.lpszProxyBypass != IntPtr.Zero) log.Info(" Bypass: " + Marshal.PtrToStringUni(proxyInfo.lpszProxyBypass)); return proxyServer; } if (ieProxyInfo.lpszProxy != IntPtr.Zero) { proxyServer = Marshal.PtrToStringUni(ieProxyInfo.lpszProxy); log.Info("WinHttpGetIEProxyConfigForCurrentUser Proxy Server Settings: "); log.Info(" Proxy: " + proxyServer); if (ieProxyInfo.lpszProxyBypass != IntPtr.Zero) log.Info(" Bypass: " + Marshal.PtrToStringUni(ieProxyInfo.lpszProxyBypass)); return proxyServer; } log.Info("No proxy server detected."); return proxyServer; } catch (Exception e) { log.Error("P/Invoke to discover proxy settings failed: " + e.ToString()); return proxyServer; } finally { if (ieProxyInfo.lpszProxy != IntPtr.Zero) try { Marshal.FreeHGlobal(ieProxyInfo.lpszProxy); } catch(Exception e) { log.Error(e.ToString()); } if (ieProxyInfo.lpszProxyBypass != IntPtr.Zero) try { Marshal.FreeHGlobal(ieProxyInfo.lpszProxyBypass); } catch(Exception e) { log.Error(e.ToString()); } if (proxyInfo.lpszProxy != IntPtr.Zero) try { Marshal.FreeHGlobal(proxyInfo.lpszProxy); } catch(Exception e) { log.Error(e.ToString()); } if (proxyInfo.lpszProxyBypass != IntPtr.Zero) try { Marshal.FreeHGlobal(proxyInfo.lpszProxyBypass); } catch(Exception e) { log.Error(e.ToString()); } if (handle != IntPtr.Zero) try { if (WinHttpCloseHandle(handle) == 0) log.Error("WinHttpCloseHandle Returned Error: " + Marshal.GetLastWin32Error()); } catch(Exception e) { log.Error(e.ToString()); } } } }
-------------------------------- From: Phil Hochstetler
|
|
|
| |
|
| |
|
|
|
|
|
|
|