The request failed with HTTP status 407: authenticationrequired.

user3889952 picture user3889952 · Jul 30, 2014 · Viewed 11.4k times · Source

Ok folks .. heres the deal -->

I have consumed a web-service in my console app and am able to successfully invoke the same when running from my local machine.

Trouble brews when I deploy the app on my Win 2K8 box. I first came up on -> The remote name could not be resolved: 'www.www.net' -> and so I added a WebProxy and hooked it up to the .Proxy property of the web-service.

The next error I have run into is -> The request failed with HTTP status 407: authenticationrequired. And I am at a loss of ideas to resolve the same.

I have used --> .Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; and have also explicitly defined proxy creds -> username, password, domain --> but to no avail

The web-service works fine when accessed over a browser from within the server.

Any help would be highly appreciated.

Thanks for your time.

Rgds,

Answer

Jeroen1984 picture Jeroen1984 · Jul 30, 2014

Are you behind a proxyserver? Does your browser have a proxy server configured? 407 means "Proxy Authentication Required", so you need to provide credentials with which you can authenticate on the proxy server. My first guess is you need someting like this.

  var credentials = new NetworkCredential("username", "password", "domain");
  WebProxy proxy = new WebProxy("YourProxyServerUri", true) { Credentials = credentials };

And then you should use te proxy object as Proxy server for your webservice.

You can also use the systems configured proxy server:

  WebProxy myProxy = (WebProxy) System.Net.HttpWebRequest.GetSystemWebProxy();

Or you could even set it up in web.config (but i think it's not possible to provide credentials here:

 <system.net> 
      <defaultProxy useDefaultCredentials="true"> 
         <proxy usesystemdefault="False" proxyaddress="YourProxyServerUri"                
          bypassonlocal="True" /> 
      </defaultProxy> 
 </system.net>

If all works fine, you may want to provide some way to configure the proxyserver and credentials at runtime, from outside your code. Otherwise you always have to recompile things if something in your production environment or location changes. You can put this in your appSettings section in web.config, in a database, app.config etc..

Also be sure you provide the right port number in your settings (for example 192.168.1.100:8888)