This is one area of .net software that just feels like a step backwards. It's all so 1997. Every single piece of .net software I've got that needs to talk to the internet requires me to set up the proxy.

Win32 developers used the wininet libraries. These are the libraries that support Internet Explorer, and so have built in support for Internet Explorer's proxy settings, including auto detection and .pac scripts.

.net is not built on top of wininet. Not quite sure why. My best guess is that (amongst other things) most uses of http web requests in .net were expected to be web services, rather than web pages, so wininet's caching was an unnecessary overhead. Whatever the reason, the support for proxy settings isn't exactly as transparent as wininet.

When you created a web request in .net 1.1, the framework would create a proxy for you, based on the values in the config file. You could explicitly define all the properties, or you could use the useSystemDefault attribute to use Internet Explorer's proxy settings.

There were several problems with this approach. IIRC, if the configuration section wasn't in your app.config, you didn't get a proxy - at all (I could be wrong on this, so don't quote me). Secondly, if the IE settings changed, this proxy class (which was cached, by the way) didn't update, so you were stuffed until you restarted your app. Thirdly, automatic proxy settings, based on .pac files, were ignored. And finally, and more importantly for me, no credentials were assigned.

Yep. I'm behind an authenticating proxy. Sucks, doesn't it?

To fix this, you had to explicitly assign credentials to the proxy. If the developer thought about proxies at all, they'd usually forget about this bit. In fact, if the developer did think about proxies, then you were usually in more trouble. They would be more explicit about everything, and leave you nothing to override in the config files. They'd go so far as to expect usernames and passwords to be hard coded in config.

.net 2 improved things a lot. Because I've just spent a good portion of this morning tracing through this looking for a bug I'm going to spell out what happens:

When you create an instance of HttpWebRequest, a default proxy is created for you, based on config. By far your best bet is to accept this, do nothing else and move on.

There's plenty you can do with the config. You can specify everything explicitly, or you can set useSystemDefault=false and get an empty proxy (not too much use. It always treats a url as though it bypasses the proxy, but it does allow you to set credentials). You can even pass in a type that gets spun up via reflection for complete flexibility. Or, you can get a proxy based on IE's settings (useSystemDefault=true).

This last one is quite impressive. It now understands automatically detecting settings and .pac files. It also (finally) responds to changes. And it even respects IE's policies and gets the settings per user or per machine. Another thing to note is that the web proxy is created in the context of the original process, not that of any impersonated users.

The best bit is that it also assigns default credentials to WebProxy.Credentials if you set useDefaultCredentials="true". These default credentials are, effectively, your credentials, and are enough to get you past an authenticating proxy. Hooray!

The slight downside is that this value isn't set by default - I guess for security, you don't want people sending credentials without cause. You can always set it in app.config or machine.config.

That's a lot of flexibility, and all from config. Alternatively, you can set the proxy in code. There are some caveats, so the moral is - don't do this. Don't set a proxy explicitly. Just leave it alone and accept the defaults. They should be flexible enough for the majority of situations.

But if you must set them in code, let's have a look at those caveats. What are your options?

First, the GlobalProxySelection class (originally seen in .net 1.1) allows you to get an empty proxy or returns WebRequest.DefaultWebProxy.

WebRequest.DefaultWebProxy will return either the nice config based class, or a static instance of a proxy class that you set in code - that's right, you can override the config based class. If you do, make sure you get it right.

WebRequest.GetSystemWebProxy() will return the same proxy you'd get if you'd specified useSystemDefault=true in config. However, it short circuits the config file, and it doesn't get credentials. Use this only if you have to (you know, like someone has a gun to your head).

WebProxy.GetDefaultProxy() is deprecated. In .net 1.1 it used to get the IE proxy settings (freshly, each time, which works around the stale settings problem). In .net 2, it's been rerouted to get the proxy settings from IE, but it doesn't touch config and it doesn't get credentials. Please don't use this one.

If you want to set credentials on a proxy, you can specify an explicit set by passing username, domain and password into a new instance of NetworkCredential. Or you can use CredentialCache.DefaultCredentials (which will get you your own credentials). Then set the Credentials property on the WebProxy you've created/retrieved.

A big gotcha is setting WebRequest.UseDefaultCredentials or WebRequest.Credentials. If you've got a proxy assigned to the WebRequest (and by default, you will have) these fields are not used.

A somewhat sneakier gotcha is that the bypass list is processed slightly differently. If you have a bypass list of e.g. *.sticklebackplastic.com, you're saying that you want to bypass the proxy for all subdomains of sticklebackplastic.com. Unfortunately, if you're trying to hit a different port e.g. my.sticklebackplastic.com:8443, this will not match, even though it does in IE. You'll need to add a rule of *.sticklebackplastic.com:8443. This was the big bug that I was chasing today (well, that and the fact that no credentials were set in the config file).

So, there we have it. Short answer is - don't set your own proxy in code and set useDefaultCredentials="true" in the config if you need it.

Related posts

Poxy proxy problem programsAfter describing how .net actually handles proxies, I thought I'd let you know the two problem progr...IE8’s poor man’s adblockOne of the more interesting-but-you’ll-never-use-it features of IE8 was the InPrivate Filtering mode...

Hi. You can set the proxy settings in code. I've just re-read this post, and it wasn't clear in a few places, so I've updated it a little.

You've got quite a few options of getting a proxy from code. They're pretty much all listed above. Once you've got one (empty, based on a config file or just a new instance of WebProxy) you can change it by setting any property you see fit. To set the credentials, set the Credentials property. This will probably be an instance of NetworkCredential, or CredentialCache.DefaultCredentials.

15. Just want to say your article is as surprising. The clearness in your post is just excellent and i can assume you're an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.

hi!,I like your writing so so much! proportion we communicate extra about your post on AOL? I require a specialist in this area to unravel my problem. May be that's you! Taking a look forward to see you.

hi!,I like your writing so so much! proportion we communicate extra about your post on AOL? I require a specialist in this area to unravel my problem. May be that's you! Taking a look forward to see you.

This is a great, great, great post. The greatest ever on .NET, imo. Thanks for your insight into this problem. The solution is so simple, but after all these years the solution is also very little known. And the implications of this are profound, in a bad way. The amount of wasted time is staggering.

Let me be the first to say: Congrats, Neil! I've been following this site from day one, and have loved every single day of awesomeness that it has delivered. You deserve all the success coming your way. Keep it up!AWESOME!

Well good for you! I have a lot but one I just thought of was when you lick a crease of paper so that it'll rip in a straight line and it actually tears in a frayed straight line rather than rips awkwardly. I love that bleachy taste and that bleachy satisfaction.

I have been exploring for a little bit for any high quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i’m happy to convey that I have an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make sure to do not forget this web site and give it a glance regularly.

Thanks , I have just been searching for info approximately this subject for a while and yours is the best I have discovered till now. But, what about the conclusion? Are you positive in regards to the supply?

Simply wanna remark on few general things, The website pattern is perfect, the subject material is rattling great. “War is much too serious a matter to be entrusted to the military.” by Georges Clemenceau.

Simply wanna remark on few general things, The website pattern is perfect, the subject material is rattling great. “War is much too serious a matter to be entrusted to the military.” by Georges Clemenceau.

Hello there! I could have sworn I’ve been to your blog before but after browsing through some of the posts I realized it’s new to me. Anyhow, I’m definitely delighted I came across it and I’ll be bookmarking it and checking back often!

I must express my appreciation to you just for rescuing me from this particular incident. As a result of exploring throughout the world wide web and finding proposals that were not beneficial, I was thinking my entire life was over. Existing minus the approaches to the problems you’ve resolved through this blog post is a serious case, as well as ones that could have in a wrong way damaged my career if I hadn’t noticed your blog post. Your good ability and kindness in playing with the whole lot was vital. I am not sure what I would have done if I had not encountered such a thing like this. It’s possible to at this moment relish my future. Thank you very much for this professional and effective guide. I won’t be reluctant to refer the website to anybody who requires assistance about this topic.

I want to voice my passion for your kindness for men and women that require help on this one subject matter. Your very own commitment to passing the message all through has been exceptionally valuable and has consistently helped individuals just like me to achieve their objectives. Your new important report denotes so much to me and substantially more to my fellow workers. Thanks a lot; from all of us.

I wish to convey my affection for your generosity supporting those who require help on this one subject matter. Your personal commitment to passing the message all-around came to be surprisingly beneficial and has continually enabled girls like me to achieve their dreams. The interesting advice indicates much a person like me and additionally to my fellow workers. Regards; from everyone of us.

You could certainly see your enthusiasm in the paintings you write. The sector hopes for even more passionate writers like you who aren’t afraid to mention how they believe. At all times follow your heart.

I have to show my respect for your kindness supporting men who should have help on the concept. Your special commitment to getting the solution along was particularly important and have constantly allowed girls just like me to arrive at their objectives. This valuable key points entails this much a person like me and substantially more to my office workers. Thanks a lot; from all of us.

I do believe all the ideas you’ve offered on your post. They’re really convincing and can certainly work. Nonetheless, the posts are too short for newbies. May just you please prolong them a bit from subsequent time? Thanks for the post.

I really like your wordpress? design, wherever do you obtain it from? regardless have you heard about this cool technlogy similar bitcoin? I searched for TrumpCoin and this netpage came up TrumpCoin.info I believe it would be a interesting point of discussion for your readers Cheers from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I love your blogging theme, exactly where would you down load it from? regardless have your readers heard about this cool thing similar bitcoin? I googled TrumpCoin and this website came up TrumpCoin.info I believe it would be a great point of discussion for your readers Cheers from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I love your wordpress web template, exactly where did you obtain it from? anyways have your blog readers heard about this unique technlogy similar bitcoin? I googled TrumpCoin and this page came up TrumpCoin.info I think it would be a thought provoking point of discussion for your readers thanks from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I really like your wordpress template, wherever did you obtain it from? eitherway have your readers heard about this cool coin similar bitcoin? I googled TrumpCoin and this netpage came up TrumpCoin.info I think it would be a great point of discussion for your readers thanks from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I really like your blogging web template, wherever would you down load it through? either way have your blog readers heard about this weird coin like bitcoin? I googled TrumpCoin and this site came up TrumpCoin.info I think it would be a thought provoking point of discussion for your readers Cheers from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I love your site theme, exactly where did you down load it from? anyways have your readers heard about this unique coin similar bitcoin? I searched for TrumpCoin and this site came up TrumpCoin.info I believe it would be a great point of discussion for your readers ciao from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

I love your webiste theme, exactly where would you down load it through? either way have your blog readers heard about this interesting technlogy like bitcoin? I searched for TrumpCoin and this netpage came up TrumpCoin.info I think it would be a good point of discussion for your readers ciao from England! <a href="www.trumpcoin.info">trumpcoin</a&gt;

Thank you for any other informative site. The place else may just I get that type of info written in such a perfect way? I’ve a venture that I am just now running on, and I’ve been on the look out for such information.

I love your site theme, where would you down load it through? back on track have you heard about this unique technlogy similar bitcoin? I searched for TrumpCoin and this page came up TrumCoin.infoI think it would be a interesting point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I appreciate your wordpress web template, where do you obtain it through? eitherway have you heard about this unique technlogy similar bitcoin? I googled TrumpCoin and this blog came up TrumCoin.infoI believe it would be a good point of discussion for your readersthanks from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I really like your wordpress web template, exactly where did you obtain it through? either way have your blog readers heard about this interesting currency like bitcoin? I googled TrumpCoin and this site came up TrumCoin.infoI think it would be a good point of discussion for your readersCheers from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I adore your wordpress web template, where do you obtain it through? either way have your readers heard about this cool coin like bitcoin? I googled TrumpCoin and this site came up TrumCoin.infoI think it would be a thought provoking point of discussion for your readersthanks from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I love your wordpress web template, exactly where did you get a hold of it from? regardless have your blog readers heard about this weird currency similar bitcoin? I googled TrumpCoin and this url came up TrumCoin.infoI think it would be a good point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I appreciate your blogging template, where would you get a hold of it through? regardless have your readers heard about this crazy tech similar bitcoin? I googled TrumpCoin and this website came up TrumCoin.infoI think it would be a thought provoking point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I appreciate your blogging design, where would you down load it through? either way have you heard about this crazy technlogy similar bitcoin? I searched for TrumpCoin and this website came up TrumCoin.infoI believe it would be a great point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I adore your wordpress? template, where do you download it through? anyways have you heard about this interesting currency like bitcoin? I googled TrumpCoin and this website came up TrumCoin.infoI think it would be a thought provoking point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I really like your site theme, where do you obtain it through? back on track have you heard about this crazy currency similar bitcoin? I googled TrumpCoin and this website came up TrumCoin.infoI believe it would be a interesting point of discussion for your readersthanks from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I adore your webiste theme, wherever do you download it through? either way have your blog readers heard about this interesting coin similar bitcoin? I searched for TrumpCoin and this site came up TrumCoin.infoI think it would be a interesting point of discussion for your readersCheers from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I love your site template, exactly where do you obtain it through? back on track have your blog readers heard about this crazy thing like bitcoin? I searched for TrumpCoin and this blog came up TrumCoin.infoI think it would be a great point of discussion for your readersCheers from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I adore your site theme, wherever do you down load it through? anyways have you heard about this unique technlogy similar bitcoin? I googled TrumpCoin and this url came up TrumCoin.infoI believe it would be a thought provoking point of discussion for your readersciao from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I adore your wordpress? design, where did you download it from? either way have your blog readers heard about this unique technlogy similar bitcoin? I searched for TrumpCoin and this blog came up TrumCoin.infoI believe it would be a great point of discussion for your readersthanks from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

I really like your wordpress theme, wherever do you get a hold of it through? either way have your readers heard about this weird thing similar bitcoin? I searched for TrumpCoin and this netpage came up TrumCoin.infoI believe it would be a good point of discussion for your readersthanks from England! Here's a few great resources <a href="htttp://www.trumpcoin.info">trumpcoin</a&gt; & <a href="htttp://www.trumpcoin.com">trumpcoin llc</a>

If it doesn't show there must be error in your Android SDK path. You need to wear apparel and shoes that allow you to move quickly for a long period of time. Thanks to Burnaby mobile app, Richmond android development this PC application is now available on mot phones. You can play number of games and also can have looked at the current news of all over the world very easily. As smartphones are becoming more and more popular with mobile device customers, businesses are realizing the need to make their content available online.

I hosted my new site from powweb, and did a little work on it, then I downloaded joomla and I am navagating through joomla trying to find how to upload my material from powweb? I tried to get into powweb website maker and it doesn't work... obviously because I downloaded joomla. Now, I also can't access joomla from powweb, I have to go to the joomla website. If someone could help me out that would be great because i'm very confused!!!!!! This is my first time ever making a site so... speak english! Thanks!.

My husband loves to write and he love sports?He needs a part time job so he was wonder if he can get paid doing what he love. How can he get paid blogging or writing about sports?. Thanks,He love and know allot about soccer or futball,and american football..

I am following often your posts and I noticed that every post contains an informative value. I have benefited from your website in many ways and I suggested this site to my friends.There is no doubt that they will like it.

I have a wordpress blog with a lot of pictures hosted on third party websites. I want all the pictures to be hosted in my wordpress blogs. I don't want to manually download all pictures and replace them in the posts, i need something to do that automatically..

My home computer is hooked up to a router. I would like to have access to my home computer's webcam through my work computer. . My home computer is hooked up through a DSL modem and is always on.. My work computer obviously has access to the Internet..

So I'm obviously new to the site. I figured out how to customize my blog page but now I need some help learning how to delete the posts that seem to automatically be there as examples and create my own..

So how can I delete an activity from my blogspot profile? Thanks <3. So I just followed another persons blog and then I clicked on my profile from the list of her followers and I want to delete one of my activities from there but I don't see a delete button? Where is it?.

I need to start a website what needs to be dynamic in regards to content and ability to run auctions and connect to payments functionalities. Can anyone advised of a suitable application / ISP/Hosting/ practical guides to allow me to under take this progress.. . Regards. . Khalidnz.

Wordpress is up and running on my host -- but it completely mismatches my site's existing theme/CSS. How hard would it be to modify/write a new theme to make it fit in? Is there an easier way to do this?.

On my friend's blogs they have added me on their blog rolls, but mine always sits at the bottom of the list and does not list when I post like it does for others. Is this a setting that I need to change or is this a choice that they have made?.

<p>I haven’t checked in here for a little while since I thought it was getting<br />uninteresting, but the last few posts are really good quality thus I guess I’ll add you back to my daily bloglist.<br />You deserve it my buddy.</p>