I have a lot of problems connecting to the Internet in the program I'm working on, and all this seems to be due to some problems with the proxy settings. Most of the problems at this stage have been fixed, but the problem that I am facing now is that my method of testing proxy server settings makes some users wait for long periods of time.
That's what I'm doing:
System.Net.WebClient webClnt = new System.Net.WebClient(); webClnt.Proxy = proxy; webClnt.Credentials = proxy.Credentials; byte[] tempBytes; try { tempBytes = webClnt.DownloadData(url.Address); } catch {
This is the only way to verify the proxy settings are correct. I tried calling the web service, but the proxy settings are not required when making the call. It will work even if I have dummy proxy settings. The above method, however, does not have a timeout member that I can set what I can find, and I use DownloadData as opposed to DownloadDataAsync, because I need to wait until the method is executed so that I can know if they are correct settings before continuing with the program.
Any suggestions regarding a better method or work for this method are welcome.
Mike
EDIT: I tried something else but no luck. I used the DownloadDataAsync method to download data in a separate stream that causes the DownloadDataCompleted WebClient to end. Although I wait for the event to be triggered, I have a loop: while (DateTime.Now <downloadStart.AddMinutes (timeout) & &! TestIsDone) {} The DownloadDataCompleted event sets the TestIsDone member to true when the event is called, The problem here is that proxy settings are bad. An event is never raised, no exception is thrown, and the program waits for the entire waiting period before continuing. Here is the code for this approach:
public static bool TestProxy(System.Net.WebProxy proxy) { ProxySettingsTestDone = false; //public static var string address = //url to some arbitrary data on our server System.Net.WebClient webClnt = new System.Net.WebClient(); webClnt.Proxy = proxy; webClnt.Credentials = proxy.Credentials; try { webClnt.DownloadDataCompleted += new System.Net.DownloadDataCompletedEventHandler(DownloadDataCallback); webClnt.DownloadDataAsync(new Uri(address)); //Timeout period DateTime dnldStartTime = DateTime.Now; while (DateTime.Now < dnldStartTime.AddMinutes(1.0) && !ProxySettingsTestDone) { } if (!ProxySettingsTestDone) //Exceded timeout { throw new System.Net.WebException("Invalid Proxy Settings"); } } catch (System.Net.WebException e) { if (e.Status == System.Net.WebExceptionStatus.ProxyNameResolutionFailure) { //Proxy failed, server may or may not be there Util.ConnectivityErrorMsg = e.Message; return false; } else if (e.Status == System.Net.WebExceptionStatus.ProtocolError) { //File not found, server is down, but proxy settings succeded ServerUp = false; Util.ConnectivityErrorMsg = e.Message; return true; } return false; } Util.ConnectivityErrorMsg = ""; return true; } private static void DownloadDataCallback(object sender, System.Net.DownloadDataCompletedEventArgs e) { if (!e.Cancelled && e.Error == null) ProxySettingsTestDone = true; else throw new System.Net.WebException("Invalid Proxy Settings"); }
Sorry for the long post. I wanted to update this question with the information I found after testing this new approach.
Thanks Mike
c # proxy
Mike webb
source share