Web Services and Proxy Servers

I'm currenting working with a team that is putting the finishing touches on a commercial application that uses web services as a major component of the infrastructure.  As part of putting the client portion through its paces, an environment that included a proxy server was tested.  The initial result was not surprising to anyone who has significant experience with web services:  a connection could not be made exception was thrown. The reason for this problem is that the method for defining a proxy server to the web service proxy class on the client side is a little unexpected.  And I apologize in advance for having to use the same word (proxy) to describe to separate items (the server and the web service class). 

If you are running Internet Explorer, then the place where the the proxy server settings are configured is in the Tools | Internet Options dialog.  Within the dialog, click on the Connections tab and the LAN Settings button.  Here you can specify whether proxy server settings are required and which server and port are used.  You can also identify whether local addresses are exempt from the proxy process.  This is fairly well known information, especially if you normally operate from behind a proxy server.

The disconnect occurs when you make calls to web services from this same machine.  Under the covers, the HttpWebRequest class is used to transmit the call to the desired server.  However, even when the proxy server information has been specified as just described, those details are NOT used by HttpWebRequest.  Instead, by default, no proxy is used to handle the request.  This is what causes the connection exception. I know the first time I ran into this issue was a surprise to me.

There are two possible solutions to this problem.  The first is to assign a WebProxy object to the Proxy property on the web service proxy class.  You can programmatically specify the proxy server information, but that is very difficult to modify.  Instead of creating the WebProxy object on the fly, use the GetDefaultProxy static method on the System.Net.WebProxy object.  This method reads “the nondynamic proxy settings stored by Internet Explorer 5.5” and returns them as a WebProxy object that can be assigned to the Proxy property .  This technique works fine assuming that your client has IE installed and that none of the scripts run at login time have modified the proxy settings.

There is an alternative that is a little more flexible.  There is a app.config element called defaultProxy that is part of the System.Net configuration section.  Within this element, the details of the proxy server can be specified independently of the IE settings.  The basic format of the defaultProxy element can be seen below.

<system.net>
   <defaultProxy>
      <proxy
         proxyaddress = "http://proxyserver:80"
         bypassonlocal = "true"
      />
   </defaultProxy>
</system.net>

The proxy element also includes an attribute called usesystemdefault which, when set to true, causes the IE settings to be used.  The benefit of this approach is that no additional coding is required.  The settings defined in defaultProxy are use by HttpWebRequest (and, therefore, web service calls) with no change to the application required.

As an added benefit, this element can be added to machine.config, which allows it to be applied to every HttpWebRequest made from the client machine regardless of the application.  Keeping in mind that an uneducated developer could assign the Proxy object in code, overriding this nice, easily configurable default.

Comments

  • bruce February 9, 2005 12:58 AM

    Thanks for posting this. It just saved my hide.

  • bruce February 10, 2005 3:55 PM

    One complication that arises with GetDefaultProxy is that, if the .config (app if there is one, or machine elsewise) setting is usesystemdefault="true", but IE is configured to get its proxy settings dynamically (auto-detect, or via a script), then you *still* get nothing from GetDefaultProxy.

    In that case I believe adding a proxyaddress= attribute is your only solution.

    Also, it looks to me like (this is on win2k w/.net 1.1) if you do specify a proxyaddress= attribute that web proxies inherit this automatically--so there's no need to call GetDefaultProxy. Can anybody (dis)confirm this for me?

    Thanks!

    -Roy

  • bruce March 3, 2005 12:53 AM

    Thank you for the posts. I am having similar problem with getting around the dynamic proxy settings. So far, the only way is by defining it directly in code or in the machine.config.

    The problem is that our application is distributed to the customers, and I do NOT know their proxy settings upfront. In terms of support, it is a big problem trying to get users to find the actual proxy that is being used.

    Did anybody find a way around this problem?

  • bruce March 5, 2005 9:17 AM

    I'm not sure what you mean by 'getting around the dynanic' setting. If all you're trying to do is use their default settings (and you know they have IE installed), then defining the proxy tag as:

    <proxy usesystemdefault="true" />

    should be sufficient.

    Am I missing something?

  • bruce October 6, 2005 6:22 PM

    If they have dynamic proxies then the usesystemdefault attribute will not work. I spent quite a bit of time working around this issue and finally came up with a way to determine the current dynamic proxy settings at runtime... take a look at the following functions:

    WinHttpGetProxyForUrl
    WinHttpGetIEProxyConfigForCurrentUser

    Using these two calls you can get the proxy required for a given URL.

  • bruce October 26, 2005 9:26 PM

    Hi,

    It seems like the defaultProxy setting in App.config is only good for non-authenticating proxies. If the proxy requires authentication, then it doesn't look I still have to create an instance of WebProxy and set it to the GlobalProxySelection.Select(). Is this correct or am I missing something? Thanks.

  • bruce November 10, 2005 9:41 AM

    k

  • bruce December 9, 2005 5:18 PM

    I am also having troubles getting the web proxy to authenticate. I can access the internet just fine through IE, but when trying to use the defualt credential cache, it will not work within my application. Any ideas?

  • bruce December 29, 2005 8:44 PM

    com

  • bruce March 6, 2006 5:29 AM

    Dim myService As New Net.webservicex.www.BibleWebservice()
    Dim proxyObject As New System.Net.WebProxy("http://www.yourproxy.com:80/", True)
    myService.Proxy = proxyObject
    myService.GetBookTitles()
    Return myService.GetBookTitles

  • bruce March 6, 2006 5:31 AM

    1. Check your proxy setting in IE.
    2. Copy the proxy address and paste in the code below. and try the code

    Dim myService As New Net.webservicex.www.BibleWebservice()
    Dim proxyObject As New System.Net.WebProxy("http://yourproxy.com:80/", True)
    myService.Proxy = proxyObject
    myService.GetBookTitles()
    Return myService.GetBookTitles

    this will definately solve the problem

  • bruce March 24, 2006 10:10 AM

    i am trying to use this site at school and cant load any proxy's as they have banned them and i cant find any free virtual browsers. THey also have restrictions for .exe and other application so i was wondering if anyone knew a why to bypass the restrictions and use Http://www.myspace.com

  • bruce April 27, 2006 12:17 PM

    i hope this works

  • bruce April 27, 2006 12:26 PM

    Union County N.C. blocked myspace and proxys and i don't know how i can get to myspace now.


    PLEASE HELP!!!!

    THANX

  • bruce May 2, 2006 12:56 PM

    ur all gay because i cant find a proxy and im stoned

  • bruce May 16, 2006 12:23 PM

    THANX

  • bruce May 23, 2006 6:12 PM

    add myspace so us kids can go on
    b/c our stupid school blocks myspace,
    i can usally find a way but not this time.
    HHHHHHHHHEEEEEEEEEEEEEEEELLLLLLLLLLLLLLP!

  • bruce July 23, 2006 11:40 PM

    please

  • bruce October 12, 2006 1:21 PM

    if u dont want to email me just post it

  • bruce October 13, 2006 2:13 PM

    You Guys do know There are too reasons why most of these Websites don't work. 1.) People Probably do Searches on google just as you did to find websites && they report them then block them from schools. 2) Everytime A wise Ass uses Myspace or finds a way at there school, if you don't DELETE THE HISTORY AFTER YOU GET ON IT, when someone checks the History they go through ALL OF THE LINKS, If they find a bad website, they report && Block it. 3) If you tell too many people how to get on, A teacher or someone who is a Tad bit retarded will spill, && tell. I say Myspace should be used at school, but no matter how hard to fight to get it, it's not going to happen. The last time my friend hacked the system to get on it, he almost got arrested. It's almost not worth it. still, If you have ways, keep them to yourselves. Sorry, that's the only way they won't ALL be blocked.

  • Sonu June 9, 2007 12:25 AM

    Hi , I am invoking a web service from another web service using httpwebrequest, please guide me in using httpwebrequest in web services.

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS