4

I want to make a request to a resource (that resource is behind a proxy). I have the proxy address and the port as well. I have tried with NetworkCredentialn no success, with CacheCredentials no success. WebException is:

ProtocolError
The remote server returned an error: (407) Proxy Authentification Required

I always get error at this line:

WebResponse response = request.GetResponse();

I have already done this: Package manager in Visual Studio 2015 "407 (Proxy Authentication Required)"

I tried to configure my App.config file

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <system.net>
         <defaultProxy useDefaultCredentials="true" />
     </system.net>
 </configuration>
Community
  • 1
  • 1
bizimunda
  • 819
  • 2
  • 9
  • 26
  • I assume "request" here is a WebRequest? Did you set the proxy property, at all? https://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy(v=vs.110).aspx or https://msdn.microsoft.com/en-us/library/system.net.webrequest.proxy(v=vs.110).aspx Within the proxy object you pass, you can set the credentials for authenticating with the proxy. – ADyson Mar 06 '17 at 13:38

1 Answers1

8

From our corporate network, we usually employ this code:

        WebProxy proxy = new WebProxy("http://your.proxy.server:8080", true);
        proxy.Credentials = new NetworkCredential("user", "password");
        WebRequest.DefaultWebProxy = proxy;

The idea is you put this code somewhere at the beginning of your program (or in the App start if you're on IIS) and then every single request will take the default proxy configuration.

No change in web.config is required. AFAICT, in web.config you cannot set the credentials.

In my experience, it works also for web services and WCF communications.

Alberto Chiesa
  • 7,022
  • 2
  • 26
  • 53
  • thank you very much for your reply but I don't want to use hard coded login and password rather would like to use user's credentials. – bizimunda Mar 06 '17 at 14:03
  • obviously you can read this settings from wherever you want (appsettings inside web.config, for example). But the thing is: the proxy setting does work for you? – Alberto Chiesa Mar 06 '17 at 14:05
  • yes proxy works for me. When I access the same page from web browser then I have no issue at all. Only issue I have is when I try to access that page from my Win form. – bizimunda Mar 06 '17 at 14:06
  • What I'm asking is: if this code works with hardcoded values, you can then figure out where you want to store them. But the question is: if you use this code with hardcoded values it does work or not? – Alberto Chiesa Mar 06 '17 at 14:08
  • let me try this and will come back – bizimunda Mar 06 '17 at 14:09
  • yes yes, when I use my credentials it works and I get the response – bizimunda Mar 06 '17 at 14:35
  • Happy to ear that. So you just need to decide where to store the credentials: in the web.config or asking the user to provide his own in a message box or somthing alike. You can find many examples for appconfig around. If this answers your question, mark it as answered. – Alberto Chiesa Mar 06 '17 at 14:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137351/discussion-between-user2983359-and-a-chiesa). – bizimunda Mar 06 '17 at 14:38
  • where to get my proxy details, when auto detect settings option is checked in internet options ? – roney Jun 14 '19 at 15:16