Friday, March 04, 2005
Web Service calls from behind proxy servers
Recently I implemented a crash reporting capability into a .Net application. When an unhandled exceptions occurs (or application ThreadException), the user is presented with a dialog prompting them to submit it as a crash report. The dialog in turn calls a web service on my web server to accept the crash data.
This worked just great, so long as the user had direct HTTP access to my web server. However, it didn't work so well when they were behind a proxy server. The specific problem was with a Squid proxy server with authentication requirements. In this case, the web service call resulted in the error: "System.Net.WebException: The request failed with HTTP status 407: Proxy Authentication Required."
It turned out that the solution was relatively straight-forward. The web service wrapper provided by Visual Studio creates a class inherited from SoapHttpClientProtocol. WebProxy.GetDefaultProxy will retrieve default IE proxy settings for use in web commands. You can then set the user's default credentials on the proxy instances using CredentialCache.DefaultCredentials. Here is some example code:
Hope this helps!
This worked just great, so long as the user had direct HTTP access to my web server. However, it didn't work so well when they were behind a proxy server. The specific problem was with a Squid proxy server with authentication requirements. In this case, the web service call resulted in the error: "System.Net.WebException: The request failed with HTTP status 407: Proxy Authentication Required."
It turned out that the solution was relatively straight-forward. The web service wrapper provided by Visual Studio creates a class inherited from SoapHttpClientProtocol. WebProxy.GetDefaultProxy will retrieve default IE proxy settings for use in web commands. You can then set the user's default credentials on the proxy instances using CredentialCache.DefaultCredentials. Here is some example code:
CrashWebServices webServices = new CrashWebServices ();
// Set the proxy settings to the default IE settings
webServices.Proxy = System.Net.WebProxy.GetDefaultProxy();
// In case proxy authentication is required,
ensure that we make the call with the user's current credentials
webServices.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
string message = webServices.SubmitCrash(email,
crashData);
Hope this helps!