web analytics

System.Net.WebException Occurred and 'HTTP status 407: Proxy Authentication Required' in C#

Options

codeling 1602 - 6666
@2016-01-07 22:42:21

In an C# application, when I try to read an RSS file from Internet using the following code:

     WebClient client = new WebClient();
   Stream rssStream = client.OpenRead(Url); 
   StreamReader textReader = new StreamReader(rssStream);
   XmlTextReader xmlReader = new XmlTextReader(textReader);
   XmlDocument xmlDoc= new XmlDocument();
   xmlDoc.Load(xmlReader);

It works fine if there is no proxy server setting, but it will report an error when there is proxy server between the local machine and Internet:

An unhandled exception of type 'System.Net.WebException' occurred in system.dll

Additional information: The remote server returned an error: 407 Proxy Authentication Required.

After some investigation to this problem, we found two solutions to read the RSS file correctly.

Using WebClient

If we use WebClient to read the RSS file, we have to tell it what the proxy server setting is, so that it knows how to read the RSS file through the proxy server. The following code demonstrates how to do this:

     System.Net.WebProxy proxy = new System.Net.WebProxy(yourproxyserver);
   proxy.Credentials = CredentialCache.DefaultCredentials;
   //if there is username/password for your proxy server setting, uncomment the following statemtnt instead.
   //proxy.Credentials = new NetworkCredential(username, password);
   GlobalProxySelection.Select = proxy;

   WebClient client = new WebClient();
   Stream rssStream = client.OpenRead(Url); 
   StreamReader textReader = new StreamReader(rssStream);
   XmlTextReader xmlReader = new XmlTextReader(textReader);
   XmlDocument xmlDoc= new XmlDocument();
   xmlDoc.Load(xmlReader);

For .NET framework, WebClient offers a new property: Proxy, so you can assign value to this property.

   client.Proxy =  proxy;

Using WebRequest

You can also use WebRequest to read the RSS file, the key point is the proxy server setting. The following code shows you the usage.

     System.Net.WebRequest req = System.Net.WebRequest.Create(Url);
   req.Proxy = new System.Net.WebProxy(yourproxyserver, true);
   req.Proxy.Credentials = CredentialCache.DefaultCredentials;
   System.Net.WebResponse resp = req.GetResponse();
   System.IO.StreamReader textReader = new System.IO.StreamReader(resp.GetResponseStream());
   XmlTextReader xmlReader = new XmlTextReader(textReader);
   XmlDocument xmlDoc= new XmlDocument();
   xmlDoc.Load(xmlReader);

@2016-01-07 22:47:55

But when I try to call a remote web service with the following C# code:

try
{
  using (RegisterForum.ForumRegistrationWebService reg = new RegisterForum.ForumRegistrationWebService()) 
  {
     _lastVersion = reg.LatestVersion();
     _lastVersionDate = reg.LatestVersionDate();
  }
}
catch(Exception ex)
{
    _lastVersion = "0";
}

The 'System.Net.WebExcpetion' is thrown with the followign error information:

"The request failed with HTTP status 407: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )."

After assigning the the proxy to the above reg variable, I get the web service called sucessfully, see the code below:

try
{
  using (RegisterForum.ForumRegistrationWebService reg = new RegisterForum.ForumRegistrationWebService())
  {
     reg.Proxy = new System.Net.WebProxy("http://yourproxyserver/", true);
     reg.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

     _lastVersion = reg.LatestVersion();
     _lastVersionDate = reg.LatestVersionDate();
  }
}
catch(Exception ex)
{
    _lastVersion = "0";
}

@2016-01-07 22:55:34

Instead of using a specific proxy, you can also use global proxy first, see the following C# code:

try
{
  using (RegisterForum.ForumRegistrationWebService reg = new RegisterForum.ForumRegistrationWebService())
  {
     reg.Proxy = System.Net.WebRequest.DefaultWebProxy;
     reg.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

     _lastVersion = reg.LatestVersion();
     _lastVersionDate = reg.LatestVersionDate();
  }
}
catch(Exception ex)
{
    _lastVersion = "0";
}

The System.Net.WebRequest.DefaultWebProxy property determines the default proxy that all WebRequest instances use if the request supports proxies and no proxy is set explicitly using the Proxy property.

The System.Net.CredentialCache.DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com