What is the preferred or accepted method of testing proxy settings?

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 { //Invalid proxy settings //Code to handle the exception goes here } 

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

+6
c # proxy
source share
3 answers

You can run proxycheck in a separate thread. And think that the check will fail if the thread takes too much time.

Or you can use WebRequest, this allows you to set a timeout:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url"); request.Proxy = proxy; request.Timeout = 2000; 

If the request did not complete within the specified timeout, a WebException value with the Status property set to WebExceptionStatus.Timeout will be selected.

+2
source share

Each method mentioned here is valid. But the most important thing is to check the proxy connection using the same Windows user account for the process that you want to test. Many proxies have special privileges for each Windows user.

+1
source share

A recent publication Testing IP: Proxies explains proxy checking with a simple Python script. The script checks for the presence and correctness of proxies.

0
source share

All Articles