tags:

views:

537

answers:

2

For the project I'm working on, we have a desktop program that contacts an online server for a store. Because it's used in schools, getting the proxy setup right is tricky. What we've gone for is to allow users to specify proxy details to use if they want, otherwise it uses the ones from IE. We've also tried to bypass incorrect details being put in, so the code tries the user specified proxy, if that fails the default one, if that fails, then with credentials, if that fails then null.

The problem I'm having is that in places where the proxy settings need to be changed in succession (for example, if their registration fails because the proxy is wrong, they change one tiny thing and try again, takes seconds.) I end up with calls to a HttpRequests .GetResponse() timing out, causing the program to freeze for a good while. Sometimes if I leave a minute or two between the changes, it doesn't freeze, but not every time (Just tried again now after 10mins and it's timing out again).

I can't spot anything in the code that could cause this - though it looks a bit messy. I don't think it could be the server refusing the request unless it's generic server behaviour as I've tried this with requests to our server and others such as google.co.uk.

I'm posting the code in the hope that someone may be able to spot something that's wrong with it, or knows a much simpler way of doing what we're trying to.

The tests we run are without any proxy, so the first part is usually skipped. The first time ApplyProxy is run, it works fine and finishes everything in the first try block, the second, it can either timeout on the GetResponse in the first try block and then go through the rest of the code, or it can work there and timeout on the actual requests made for the registration.

Code:

void ApplyProxy() {

        Boolean ProxySuccess = true;
        String WebRequestURI = @"http://www.google.co.uk";

        if (UseProxy)
        {
            try
            {
                String ProxyUrl = (ProxyUri.ToLower().Contains("http://")) ?
                    ProxyUri :
                    "http://" + ProxyUri;

                WebRequest.DefaultWebProxy = new WebProxy(ProxyUrl);
                if (!string.IsNullOrEmpty(ProxyUsername) && !string.IsNullOrEmpty(ProxyPassword))
                    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch
            {
                ProxySuccess = false;
            }
        }
        if(!ProxySuccess || !UseProxy)
        {
            try
            {
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            { //try with credentials
                //make a new proxy from defaults
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                String newProxyURI = WebRequest.DefaultWebProxy.GetProxy(new Uri(WebRequestURI)).ToString();
                if (newProxyURI == String.Empty)
                { //check we actually get a result
                    WebRequest.DefaultWebProxy = null;
                    return;
                }
                //continue
                WebProxy NewProxy = new WebProxy(newProxyURI);
                NewProxy.UseDefaultCredentials = true;
                NewProxy.Credentials = CredentialCache.DefaultCredentials;
                WebRequest.DefaultWebProxy = NewProxy;

                try
                {
                    HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                    request.Method = "GET";
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                }
                catch
                {
                    WebRequest.DefaultWebProxy = null;
                }
            }

        }
    }
A: 

Is it not just a case of needing to set the Timeout property of the HttpWebRequest? It could be that the connection is being made, but not serviced (wrong type of proxy server or stalled server, for example), in which case it may be that the request is waiting for the Timeout period before giving up — a shorter timeout may be preferrable here.

Paul Ruane
Unfortunatly, the requests aren't serviced, using the example of registration, nothing comes back when there should be a success or failure message.
Septih
A: 

Seems to be a programming error on my behalf. The requests were left open and obviously either the program or the server doesn't like this. Simply closing the HttpWebRequests once done seems to remove this issue.

Septih