Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Opening the Internet Settings Dialog and using Windows Default Network Settings via Code


:P
On this page:

Ran into a question from a client the other day that asked how to deal with Internet Connection settings for running  HTTP requests. In this case this is an old FoxPro app and it's using WinInet to handle the actual HTTP connection. Another client asked a similar question about using the IE Web Browser control and configuring connection properties.

Regardless of platform or tools used to do HTTP connections, you can probably configure custom connection and proxy settings in your application to configure http connection settings manually. However, this is a repetitive process for each application requires you to track system information in your application which is undesirable.

Often it's much easier to rely on the system wide proxy settings that Windows provides via the Internet Settings dialog. The dialog is a Control Panel applet (inetcpl.cpl) and is the same dialog that you see when you pop up Internet Explorer's Options dialog:

internetsettings

This dialog controls the Windows connection properties that determine how the Windows HTTP stack connects to the Internet and how Proxy's are used if configured. Depending on how the HTTP client is configured - it can typically inherit and use these global settings.

Loading the Settings Dialog Programmatically

The settings dialog is a Control Panel applet with the name of:

inetcpl.cpl

and you can use any Shell execution mechanism (Run dialog, ShellExecute API, Process.Start() in .NET etc.) to invoke the dialog. Changes made there are immediately reflected in any applications that use the default connection settings.

In .NET you can simply do this to bring up the Internet Settings dialog with the Connection tab enabled:

Process.Start("inetcpl.cpl",",4");

In FoxPro you can simply use the RUN command to execute inetcpl.cpl:

lcCmd = "inetcpl.cpl ,4"
RUN &lcCmd

Using the Default Connection/Proxy Settings

When using WinInet you specify the Http connect type in the call to InternetOpen() like this (FoxPro code here):

hInetConnection=;
   InternetOpen(THIS.cUserAgent,0,;
   THIS.chttpproxyname,THIS.chttpproxybypass,0)

The second parameter of 0 specifies that the default system proxy settings should be used and it uses the settings from the Internet Settings Connections tab. Other connection options for HTTP connections include 1 - direct (no proxies and ignore system settings), 3 - explicit Proxy specification. In most situations a connection mode setting of 0 should work.

In .NET HTTP connections by default are direct connections and so you need to explicitly specify a default proxy or proxy configuration to use. The easiest way to do this is on the application level in the config file:

<configuration>
  <system.net>
    <defaultProxy>
      <proxy bypassonlocal="False" autoDetect="True" usesystemdefault="True" />
    </defaultProxy>
  </system.net>
</configuration>

You can do the same sort of thing in code specifying the proxy explicitly and using System.Net.WebProxy.GetDefaultProxy(). So when making HTTP calls to Web Services or using the HttpWebRequest class you can set the proxy with:

StoreService.Proxy = WebProxy.GetDefaultProxy();

All of this is pretty easy to deal with and in my opinion is a way better choice to managing connection settings than having to track this stuff in your own application. Plus if you use default settings, most of the time it's highly likely that the connection settings are already properly configured making further configuration rare.

Posted in Windows  HTTP  .NET  FoxPro  

The Voices of Reason


 

James C
July 20, 2011

# re: Opening the Internet Settings Dialog and using Windows Default Network Settings via Code

Thanks for the info about those settings, I've seen trouble with that on client web services. Just an FYI, the image link is broken.

lojistikci
August 10, 2011

# re: Opening the Internet Settings Dialog and using Windows Default Network Settings via Code

I have wanted to find to using proxy setting. this post is great. thanks many.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024