Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!


I posted about this issue before indirectly, but I think it bears repeating as a dedicated topic. I've seen a fair number of questions about this on various forums recently. 

 

The problem: You're using some sort of HTTP client from an ASP.NET 2.0 or low rights .NET 2.0 application and it's very slow to connect to a Web server to retrieve HTTP content. This could be with WebRequest, WebClient, XmlDom/Reader or a Web Service Client.

 

In .NET 2.0 the default proxy configuration settings have changed. In .NET 1.1 the default proxy settings basically were set for no proxy, which meant that if you connected to the Web using HttpWebRequest, WebClient, an XmlDom/Reader object or a Web Service it would try to connect directly. If you wanted to use the machine default proxy settings you had to explicity assign the DefaultProxy:

 

Request.Proxy = WebProxy.GetDefaultProxy();

 

In .Net 2.0 the above behavior becomes the default implicitly. .NET 2.0 uses the machine’s default proxy configuration by default. The default proxy configuration is the configuration you see in the IE proxy configuration settings.

 

Now for most desktop applications, this is sensible – you’d want to use the system proxy settings to make a connection so your app doesn’t have to manually set proxy configuration settings. However, if you’re running a Web application or a limited rights application downloaded from the Web or otherwise running in a low security environment, you’ll run into problems the default proxy retrieval. The problem is that low access accounts don’t have access to the machine proxy configuration settings which are stored in the registry under HKLM keys.

 

So if you’re running ASP.NET with NETWORK SERVICE as your host account – you can’t get at the proxy settings. I ran into this a while back where I had some aggregate RSS feeds from various sites I was displaying on a page of my site. It was taking upwards of 10 seconds for these feeds to get retrieved even from my own site on the local machine! The problem was the proxy settings. .NET apparently tries to retrieve the proxy settings and looks in various places to get them, fails and eventually gives up and simple directly connects. It works, but it’s VERY VERY slow.

 

Luckily the solution is simple. You can override the proxy settings in the .Config file for your application using the defaultProxy key in the system.net section. The following disables automatic Proxy detection:

 

<configuration >

  <system.net>

    <defaultProxy>

      <proxy bypassonlocal="true" usesystemdefault="false" />

    </defaultProxy>

</system.net>

</configuration>

 

Another more explicit option in code is to explicitly set the Proxy property of the Http object (WebRequest, XmlDom, Web Service Client etc.) to null which essentially has the same effect.

 

Make Donation


Feedback for this Post

 
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by toprak February 09, 2006 @ 3:38am
The connection is still slow, after changed the web.config.

Is there any other solutions?

http://www.autospital.ch
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by rbowall April 24, 2006 @ 1:20pm
Great info! Spent ages trying to work out why the first http request took half a minute. Afterwards, each request was immediate - As you said just needed to add the following to my code:

Dim w as webclient
w.proxy = Nothing
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Silky Shocca May 29, 2006 @ 7:22pm
THANK YOU@!#!@#!@#!@#@!#@!@!#@!!#@#!@#

HOW CAN MICROSOFT BE SO STUPID#!@!@!@#@#!@#

I put that stuff in my config file and stuff started working fast!@#
# ASP.NET Forums - Deploying ASP.NET 2.0
by Configuration and Deployment September 28, 2006 @ 4:33pm
# DotNetSlackers: Slow Http client calls from ASP.NET 2.0 Make sure you check your Proxy Settings!
by DotNetSlackers Latest ASP.NET News October 14, 2006 @ 12:33pm
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Rajesh April 24, 2007 @ 6:32am
I am using Rssfeed control in my page. when it tries to access the web resource using IIS,
it throws the following exception
"The remote name could not be resolved: "
But in ASP.NET development server it is working fine.

In web.config file i have added the follwing entry

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


What should i do now? please help me out.
# Deploying ASP.NET 2.0 - ASP.NET Forums
by ASP.NET Forums May 22, 2007 @ 12:45pm
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Joel June 02, 2007 @ 5:41pm
Thanks - dropped the run time of my function from ~30seconds to ~2-3seconds
# K. Scott Allen : Debugging HTTP
by K. Scott Allen July 09, 2007 @ 5:37pm
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Steve R. September 28, 2007 @ 11:36am
Does anyone know what needs to be done to fix this problem in a .NET 2.0 Windows application (Windows Forms)?

Thanks,
Steve
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Rick Strahl September 28, 2007 @ 12:10pm
The same solution should apply, although in general you won't have this problem in a WinForms app because WinForms will generally have rights to get at the proxy settings.
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Nick H October 17, 2007 @ 2:59pm
Will this default behavior of .NET 2.0 also cause the w3wp.exe to work hard (high CPU usage)?

Thanks!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Mohammed Abrar March 26, 2008 @ 2:17pm
Very useful post.
pulled my hairs for long to get this.
anyways, thank's Rick
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by vanishree June 04, 2008 @ 12:37am
HI,
am very new to asp.net .. i have created one small application.. i have placed tat in iis and if am running in my system through ip am able to run .. but same thin if am running in local network using ip of my system am geeting the following error(10060-connection time out) .. i have added this code in webconfig and am disabling the proxy connection seeting when am running on my system...

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

can any one please help me..
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Jeff September 06, 2008 @ 9:15pm
I'm a little confused. I'm running a windows app, deployed using click-once, that accesses a web service. Do I make this configuration change in the applications app.config or the web services web.config file?

Thanks
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Rick Strahl September 06, 2008 @ 9:31pm
@Jeff - this should only be an issueon the server in web.config. Client side should pick up proper proxy settings.

I also think this has been addressed in .NET 3.0 and later so before you muck with this setting make sure you check and see if this is actually a problem first. Using default settings is always preferrable if they work right <s>...
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Jinal September 30, 2008 @ 9:42am
Hi,

I get same error Unable to connect remote server. I made a class library and it going to call third party webservice . Code works fine on my local server and PC.
But when i transfer it remote server it produce error. Console application works fine on remote server. But when i try to use same class lib in webapplication it throws error.

so what may be steps to resolve this issue ?

Thanks.
# Slow Http client calls from ASP.NET 2.0? Make sure you check your Prox
by DotNetKicks.com January 27, 2009 @ 6:30am
You've been kicked (a good thing) - Trackback from DotNetKicks.com
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by FR August 28, 2009 @ 1:53pm
THANK YOU RICK!!
After three hours i found the solution here:

Dim wc As HttpWebRequest = WebRequest.Create("https://api.games.betfair.com/rest/v1/channels/1444089/snapshot?username=xxx")
wc.Proxy = Nothing

it was really slow before (18sec!!)
now runs like hell!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Juan January 17, 2010 @ 10:56am
Worked!
I spent hours trying to solve this problem until I found this.
Thanks a lot!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by svein February 06, 2010 @ 1:02pm
This stupid thing really kept me pulling my hair for a while until I found your post.

Thanks a lot!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by John May 10, 2010 @ 5:09pm
Awesome post! It is still paying dividends after all these years! Helped me out a bunch. Thanks!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Jason March 14, 2011 @ 2:22pm
The xml app.config or web.config does not have a place for credentials. If you are behind a company firewall you possibly need to login with your domain credentials. This must be done in code:


//instantiate your web service
//wsStockQuotes = new StockQuoteWebService(....

//setup the proxy info for the web service
System.Net.WebProxy webProxy = new System.Net.WebProxy("domain_name_of_your_proxy_server", 8080);
webProxy.Credentials = new System.Net.NetworkCredential("your_domain_username", "your_domain_pass", "login_domain");
wsStockQuotes.Proxy = webProxy;

//now make calls to your web service
//wsStockQuotes.GetQuote(.....
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Mark Schlegel March 16, 2011 @ 7:21am
I had to change the internet options in Internet Explorer.

Tools->Internet Options->Connections->LAN Settings

I had to uncheck "Bypass proxy server for local addresses". We are using a proxy server, which is set up correctly, I just didn't need the Bypass part checked.
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Jon Badgett March 30, 2012 @ 4:53pm
Thanks for this post. The problem still exists for .NET 4 apps. I've used your site countless times and just wanted to say thanks.
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Michael September 26, 2012 @ 1:27pm
Incredible. I was stuck on this for hours. Thank you so much!
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Simon October 08, 2012 @ 10:55pm
Excellent. Thanks for this. I had a nasty that was only showing up on Windows 7. I thought it was the xml construction in the Web Service call, but that proved to be wrong. Following your post made everything happy. Many, many thanks.
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by BT April 02, 2013 @ 7:32am
Thaks so much for posting this. After three days of searching for a resolution to my "Unable To Connect to Remote Host" error in WCF with the HttpWebRequest, I actually found the solution here (http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2007/12/03/unable-to-connect-to-the-remote-server.aspx) but you helped him out so I thought I would pass along a thank you because it ultimately helped me too.
# re: Slow Http client calls from ASP.NET 2.0? Make sure you check your Proxy Settings!
by Prash January 23, 2014 @ 7:11pm
Fantastic!!..even i spent hours and days analysing slowness of my first webservice call , after setting the above configuration settings . it got rid of all those latency making my service faster.

Thanks Rick!!
 


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