I've got a program that detects if it can connect to a webservice. It sends a System.Net.WebRequest and then checks the information it gets back to findout if it can connect to the service properly. If there is an error it will catch an exception and everything is fine. This used to work wonderfully, until somebody decided they needed to use a proxy server. (rats!) It's written in C# and uses .Net 1.1. I know that .Net 2.0 fixes a bunch of stuff with this issue, but right now I don't have an option to move to 2.0 on this project. Here is the code:
Expand|Select|Wrap|Line Numbers
- if ((connected && null != ConfigurationSettings.AppSettings["PortalServer"])||(ConfigurationSettings.AppSettings["PortalServer"].StartsWith("http://localhost")))
- {
- try
- {
- // In case a proxy server is being used via the Internet Explorer Settings
- System.Net.WebProxy proxy = System.Net.WebProxy.GetDefaultProxy(); // Get default proxy settings
- if(proxy.Address != null) // If there is an address then use it
- {
- Uri proxyURI = new Uri(proxy.Address.ToString());
- System.Net.GlobalProxySelection.Select = new System.Net.WebProxy(proxyURI);
- WebProxy proxyObject = new WebProxy(proxyURI,true);
- proxyObject.Credentials = System.Net.GlobalProxySelection.Select.Credentials.GetCredential(proxyURI,"Basic");
- proxyObject.BypassProxyOnLocal = false;
- // Create a new 'HttpWebRequest' Object to the mentioned URL.
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
- ConfigurationSettings.AppSettings["PortalServer"] + "?wsdl");
- request.Method = "GET";
- request.ContentType = "application/x-www-form-urlencoded";
- request.KeepAlive = true;
- request.Timeout = 10000;
- request.ProtocolVersion = HttpVersion.Version10;
- request.Proxy = proxyObject;
- Console.WriteLine("\nThe 'Proxy Server' of the protocol used is {0}",request.Proxy.ToString());
- System.Net.WebResponse response = request.GetResponse();
- string responseText = (new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd());
- response.Close();
- connected = responseText.IndexOf("name='DataPortal'") > -1;
- lastRequestSuccessful = true;
- }
- else
- {
- System.Net.WebRequest request = System.Net.WebRequest.Create(
- ConfigurationSettings.AppSettings["PortalServer"] + "?wsdl");
- System.Net.WebResponse response = request.GetResponse();
- string responseText = (new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd());
- response.Close();
- connected = responseText.IndexOf("name='DataPortal'") > -1;
- lastRequestSuccessful = true;
- }
- }
- catch(Exception ex)
- {
- if (lastRequestSuccessful)
- log.Error("Could not connect to the data portal", ex);
- lastRequestSuccessful = false;
- connected = false;
- }
In the App.Config file I've entered the following:
Expand|Select|Wrap|Line Numbers
- <system.net>
- <defaultProxy>
- <proxy proxyaddress = "http://proxyserveraddress:80" bypassonlocal = "false"/>
- </defaultProxy>
- </system.net>
Right now I'm getting the following error:
System.Net.WebException: The operation has timed-out.
at System.Net.HttpWebRequest.GetResponse()
at CnpXpress.WindowsForms.Common.Offline.DataPortalDe tectionStrategy.IsConnected() in c:\projects\mohave\trunk\src\windowsforms.common\o ffline\dataportaldetectionstrategy.cs:line 81
Anybody have any ideas?
Thanks in advance.