tags:

views:

200

answers:

1

Another question about Web proxy.

Here is my code:

IWebProxy Proxya = System.Net.WebRequest.GetSystemWebProxy();
Proxya.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(targetServer);

rqst.Proxy = Proxya;
rqst.Timeout = 5000;
try
{
    rqst.GetResponse();
}
catch(WebException wex)
{
    connectErrMsg = wex.Message;
    proxyworks = false; 
}

This code hangs the first time it is called for a minute of two. After that on successive calls it works sometimes, but not others. It also never hits the catch block.

Now the weird part. If I add a MessageBox.Show(msg) call in the first section of code before the GetResponse() call this all will work every time without hanging. Here is an example:

try
{
    // ========Here is where I make the call and get the response========
    System.Windows.Forms.MessageBox.Show("Getting Response");
    // ========This makes the whole thing work every time========


    rqst.GetResponse();
}
catch(WebException wex)
{
    connectErrMsg = wex.Message;
    proxyworks = false; 
}

I'm baffled about why it is behaving this way. I don't know if the timeout is not working (it's in milliseconds, not seconds, so should timeout after 5 seconds, right?...) or what is going on. The most confusing this is that the message box call makes it all work without hanging.

So any help and suggestions on what is happening is appreciated. These are the kind of bugs that drive me absolutely out of my mind.




EDIT and CORRECTION:

OK, so I've been testing this and the problem is caused when I try to download data from the URI that I am getting a response from. I am testing the connectivity using the GetResponse() method with a WebRequest, but am downloading the data with a WebClient. Here is the code for that:

public void LoadUpdateDataFromNet(string url, IWebProxy wProxy)
{
    //Create web client
    System.Net.WebClient webClnt = new System.Net.WebClient();

    //set the proxy settings
    webClnt.Proxy = wProxy;
    webClnt.Credentials = wProxy.Credentials;

    byte[] tempBytes;
    //download the data and put it into a stream for reading
    try
    {
        tempBytes = webClnt.DownloadData(url); // <--HERE IS WHERE IT HANGS
    }
    catch (WebException wex)
    {
        MessageBox.Show("NEW ERROR: " + wex.Message);
        return;
    }

    //Code here that uses the downloaded data
}

The WebRequest and WebClient are both accessing the same URL which is a web path to an XML file and the proxy is the same one created in the method at the top of this post. I am testing to see if the created IWebProxy is valid for the specified path and file and then downloading the file.

The first piece of code I put above and this code using the WebClient are in separate classes and are called at different times, yet using a message box in the first bit of code still makes the whole thing run fine, which confuses me. Not sure what all is happening here or why message boxes and running/debugging in Visual Studio makes the program run OK. Suggestions?

A: 

So, I figured out the answer to the problem. The timeout for the we request is still 5 sec, but for some reason if it is not closed explicitly it makes consecutive web requests hang. Here is the code now:

IWebProxy Proxya = System.Net.WebRequest.GetSystemWebProxy();

//to get default proxy settings
Proxya.Credentials = CredentialCache.DefaultNetworkCredentials;
Uri targetserver = new Uri(targetAddress);
Uri proxyserver = Proxya.GetProxy(targetserver);

HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(targetserver);
rqst.Proxy = Proxya;
rqst.Timeout = 5000;

try
{
    //Get response to check for valid proxy and then close it
    WebResponse wResp = rqst.GetResponse();

    //===================================================================
    wResp.Close(); //HERE WAS THE PROBLEM. ADDING THIS CALL MAKES IT WORK
    //===================================================================
}
catch(WebException wex)
{
    connectErrMsg = wex.Message;
    proxyworks = false; 
}

Still not sure exactly how calling the message box was making everything work, but it doesn't really matter at this point. The whole thing works like a charm.

Mike Webb