SteveGurasich

Hello,

I've got a problem that is pretty difficult for me to nail down.

I'm developing a C# WinForms application using Visual Studio 2005.  I have a form in the application that is acting as a feedback form.  It has a few text fields, and clicking on a button will submit the information to a web page for storing in a database.  The button is a "Next" button, which shows a label where I display some information about the connection and submission process.  Once that finishes, the user can hit "Back", fill out more feedback, and hit "Next" again to submit it a second time.  Or, they could have just closed the form and pressed the Feedback button to make it show again.  Regardless, however the second request is sent, it always throws a timeout exception.  The first request works great (and very quickly), and the second always stalls.  I've posted my code below, you can see that there are 3 different blocks to make the request.  I tried each of the three ways, and all of them have the same timeout behavior.  I have tried running this function in a new thread or in the current thread and the same thing happens.  Since this is so repeatable, I'm assuming I'm missing a detail somewhere.  Please let me know if there's anything I'm doing wrong, or anything I can try to help me figure this out.  If I need to run a trace, please let me know how I can do that in VS 2005 [note: I've posted a network trace in the next post].  This is my first Windows application since graduating college in 2003, so there are several development tools that I'm not sure how to use yet.

The exception message is "The operation has timed out".  With the below code, it seems to be happening at the first req.GetResponse, the message I'm seeing that I output is "There was a problem contacting the feedback server...".  Previously, I think when I was testing this in a new thread, it was displaying the message that says "Submitting feedback..." and then timing out after that.

 


        private void submitFeedback()
        {
            HttpWebRequest req;
            HttpWebResponse resp;
            bool connected = false;

            try
            {
                req = (HttpWebRequest)WebRequest.Create("http://www.server.com/");
                resp = (HttpWebResponse)req.GetResponse();

                try
                {
                    if (resp.StatusCode.ToString().Equals("OK"))
                    {
                        lblSend1.Text += "\n\nThe feedback server at www.server.com was contacted.";
                        connected = true;
                    }
                    else
                    {
                        lblSend1.Text += "\n\nThe feedback server at www.server.com could not be contacted.  Please check your internet connection and try again.";
                        btnBack.Enabled = true;
                    }
                }
                catch (Exception exc)
                {
                    setText("\n\nThe feedback server at www.server.com could not be contacted.  Please check your internet connection and try again.");
                    setText("\n\n" + exc.Message.ToString());
                    btnBack.Enabled = true;
                }

                if (connected)
                {
                    lblSend1.Text += "\n\nSubmitting feedback...";
                    try
                    {
                        string submit_url = "http://www.server.com/mckinley/menu/submit_feedback.asp";

                        WebClient sendReq = new WebClient();

                        System.Collections.Specialized.NameValueCollection urlParams = new System.Collections.Specialized.NameValueCollection();
                        string urlUserName = feedback.userName;
                        string urlProbver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                        string urlPageName = pageName;
                        string urlComments = txtComments.Text;

                        urlUserName = URLEncode(urlUserName);
                        urlProbver = URLEncode(urlProbver);
                        urlPageName = URLEncode(urlPageName);
                        urlComments = URLEncode(urlComments);

                        urlParams.Add("name", urlUserName);
                        urlParams.Add("probver", urlProbver);
                        urlParams.Add("ml", urlPageName);
                        urlParams.Add("comment", urlComments);

                        sendReq.QueryString = urlParams;

                        string strResp = sendReq.DownloadString(submit_url);
                        lblSend1.Text += "\n\nResponse from server: " + strResp;

                       
                       
                        /* this also produced the same behavior:
                        string urlUserName = feedback.userName;
                        string urlProbver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                        string urlPageName = pageName;
                        string urlComments = txtComments.Text;

                        urlUserName = URLEncode(urlUserName);
                        urlProbver = URLEncode(urlProbver);
                        urlPageName = URLEncode(urlPageName);
                        urlComments = URLEncode(urlComments);

                        string postdata = "name=" + urlUserName + "&probver=" + urlProbver + "&ml=" + urlPageName + "&comment=" + urlComments;
                        byte[] data;

                        ASCIIEncoding encoding = new ASCIIEncoding();
                        data = encoding.GetBytes(postdata);
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(submit_url);
                        request.Method = "POST";
                        request.KeepAlive = true;
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.ContentLength = data.Length;

                        Stream newStream;
                        newStream = request.GetRequestStream();
                        newStream.Write(data, 0, data.Length);
                        newStream.Close();

                        HttpWebResponse response;
                        response = (HttpWebResponse)request.GetResponse();
                        StreamReader sr;
                        sr = new StreamReader(response.GetResponseStream());

                        string strResp = sr.ReadToEnd();
                        sr.Close();
                        response.Close();
                        */

 

                        /* this also produced the same behavior:
                        string urlUserName = feedback.userName;
                        string urlProbver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                        string urlPageName = pageName;
                        string urlComments = txtComments.Text;
                        string strResp;

                        urlUserName = URLEncode(urlUserName);
                        urlProbver = URLEncode(urlProbver);
                        urlPageName = URLEncode(urlPageName);
                        urlComments = URLEncode(urlComments);

                        string postdata = "name=" + urlUserName + "&probver=" + urlProbver + "&ml=" + urlPageName + "&comment=" + urlComments;

                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(submit_url);
                        request.Method = "POST";
                        request.ContentLength = postdata.Length;
                        request.ContentType = "application/x-www-form-urlencoded";

                        StreamWriter writer = new StreamWriter(request.GetRequestStream());
                        writer.Write(postdata);
                        writer.Close();

                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            strResp = sr.ReadToEnd();
                            sr.Close();
                        }
                        */

 

                        if (strResp == "success")
                        {
                            lblSend1.Text += "\n\nThe feedback was successfully sent.  Thank you.";
                            txtComments.Text = "";
                            btnBack.Enabled = true;
                            btnFinish.Enabled = true;
                        }
                        else
                        {
                            lblSend1.Text += "\n\nThe feedback was not added to the database.  There was a problem with the server.  Please try again.";
                            btnBack.Enabled = true;
                        }
                    }
                    catch (Exception exc)
                    {
                        setText("\n\nThe feedback could not be sent.  Please check your internet connection and try again.");
                        setText("\n\n" + exc.Message.ToString());
                        btnBack.Enabled = true;
                    }
                }
            }
            catch (Exception exc)
            {
                setText("\n\nThere was a problem contacting the feedback server at www.server.com.");
                setText("\n\n" + exc.Message.ToString());
                btnBack.Enabled = true;
            }
        }



Re: .NET Framework Networking and Communication Timeout exception after the first request

SteveGurasich

I have figured out how to run a network trace. 

This is the log for the first (successful) request:

 

System.Net Verbose: 0 : [2776] WebRequest::Create(http://www.server.com/)
System.Net Verbose: 0 : [2776] HttpWebRequest#6968762::HttpWebRequest(http://www.server.com/#-1326955484)
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#6968762::HttpWebRequest()
System.Net Verbose: 0 : [2776] Exiting WebRequest::Create()  -> HttpWebRequest#6968762
System.Net Verbose: 0 : [2776] HttpWebRequest#6968762::GetResponse()
System.Net Information: 0 : [2776] Associating HttpWebRequest#6968762 with ServicePoint#62718864
System.Net Information: 0 : [2776] Associating Connection#27598869 with HttpWebRequest#6968762
System.Net Information: 0 : [2776] Associating HttpWebRequest#6968762 with ConnectStream#47063234
System.Net Information: 0 : [2776] HttpWebRequest#6968762 - Request: GET / HTTP/1.1

System.Net Information: 0 : [2776] ConnectStream#47063234 - Sending headers
{
Host: www.server.com
Connection: Keep-Alive
}.
System.Net Information: 0 : [2776] Connection#27598869 - Received status line: Version=1.1, StatusCode=200, StatusDescription=OK.
System.Net Information: 0 : [2776] Connection#27598869 - Received headers
{
Content-Length: 1236
Cache-Control: private
Content-Type: text/html
Date: Thu, 21 Dec 2006 22:16:30 GMT
Set-Cookie: ASPSESSIONIDAQQTTRTQ=EACGAAPAFJOEHIPILFJHKCHF; path=/
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
}.
System.Net Information: 0 : [2776] ConnectStream#20915929::ConnectStream(Buffered 1236 bytes.)
System.Net Information: 0 : [2776] Associating HttpWebRequest#6968762 with ConnectStream#20915929
System.Net Information: 0 : [2776] Associating HttpWebRequest#6968762 with HttpWebResponse#54025633
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#6968762::GetResponse()  -> HttpWebResponse#54025633
System.Net Verbose: 0 : [2776] WebClient#16468652::DownloadString(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] WebClient#16468652::DownloadData(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] WebRequest::Create(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve)
System.Net Verbose: 0 : [2776] HttpWebRequest#14000148::HttpWebRequest(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#14000148::HttpWebRequest()
System.Net Verbose: 0 : [2776] Exiting WebRequest::Create()  -> HttpWebRequest#14000148
System.Net Verbose: 0 : [2776] HttpWebRequest#14000148::GetResponse()
System.Net Information: 0 : [2776] Associating HttpWebRequest#14000148 with ServicePoint#62718864
System.Net Information: 0 : [2776] Associating Connection#58892473 with HttpWebRequest#14000148
System.Net Information: 0 : [2776] Associating HttpWebRequest#14000148 with ConnectStream#60270212
System.Net Information: 0 : [2776] HttpWebRequest#14000148 - Request: GET /mckinley/menu/submit_feedback.asp name=steve&probver=1%2E0%2E0%2E5&ml=Home&comment=steve HTTP/1.1

System.Net Information: 0 : [2776] ConnectStream#60270212 - Sending headers
{
Host: www.server.com
}.
System.Net Information: 0 : [2776] Connection#58892473 - Received status line: Version=1.1, StatusCode=200, StatusDescription=OK.
System.Net Information: 0 : [2776] Connection#58892473 - Received headers
{
Content-Length: 7
Cache-Control: private
Content-Type: text/html
Date: Thu, 21 Dec 2006 22:16:30 GMT
Set-Cookie: ASPSESSIONIDAQQTTRTQ=FACGAAPAEBBOMCGKPJGOONIB; path=/
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
}.
System.Net Information: 0 : [2776] ConnectStream#5560998::ConnectStream(Buffered 7 bytes.)
System.Net Information: 0 : [2776] Associating HttpWebRequest#14000148 with ConnectStream#5560998
System.Net Information: 0 : [2776] Associating HttpWebRequest#14000148 with HttpWebResponse#50048984
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#14000148::GetResponse()  -> HttpWebResponse#50048984
System.Net Verbose: 0 : [2776] HttpWebResponse#50048984::GetResponseStream()
System.Net Information: 0 : [2776] ContentLength=7
System.Net Verbose: 0 : [2776] Exiting HttpWebResponse#50048984::GetResponseStream()  -> ConnectStream#5560998
System.Net Verbose: 0 : [2776] ConnectStream#5560998::Read()
System.Net Verbose: 0 : [2776] Data from ConnectStream#5560998::Read
System.Net Verbose: 0 : [2776] 00000000 : 73 75 63 63 65 73 73                            : success
System.Net Verbose: 0 : [2776] Exiting ConnectStream#5560998::Read()  -> 7#7
System.Net Verbose: 0 : [2776] ConnectStream#5560998::Read()
System.Net Verbose: 0 : [2776] Exiting ConnectStream#5560998::Read()  -> 0#0
System.Net Verbose: 0 : [2776] ConnectStream#5560998::Close()
System.Net Verbose: 0 : [2776] Exiting ConnectStream#5560998::Close()
System.Net Verbose: 0 : [2776] ConnectStream#5560998::Close()
System.Net Verbose: 0 : [2776] Exiting ConnectStream#5560998::Close()
System.Net Verbose: 0 : [2776] Exiting WebClient#16468652::DownloadString()  -> success

 

And this is the second (unsuccessful) request:

System.Net Verbose: 0 : [2776] WebRequest::Create(http://www.server.com/)
System.Net Verbose: 0 : [2776] HttpWebRequest#53294272::HttpWebRequest(http://www.server.com/#-1326955484)
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#53294272::HttpWebRequest()
System.Net Verbose: 0 : [2776] Exiting WebRequest::Create()  -> HttpWebRequest#53294272
System.Net Verbose: 0 : [2776] HttpWebRequest#53294272::GetResponse()
System.Net Information: 0 : [2776] Associating HttpWebRequest#53294272 with ServicePoint#62718864
System.Net Information: 0 : [2776] Associating Connection#58892473 with HttpWebRequest#53294272
System.Net Information: 0 : [2776] Associating HttpWebRequest#53294272 with ConnectStream#9886408
System.Net Information: 0 : [2776] HttpWebRequest#53294272 - Request: GET / HTTP/1.1

System.Net Information: 0 : [2776] ConnectStream#9886408 - Sending headers
{
Host: www.server.com
}.
System.Net Information: 0 : [2776] Connection#58892473 - Received status line: Version=1.1, StatusCode=200, StatusDescription=OK.
System.Net Information: 0 : [2776] Connection#58892473 - Received headers
{
Content-Length: 1236
Cache-Control: private
Content-Type: text/html
Date: Thu, 21 Dec 2006 22:16:54 GMT
Set-Cookie: ASPSESSIONIDAQQTTRTQ=GACGAAPAJAPMMDFMNENABFED; path=/
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
}.
System.Net Information: 0 : [2776] ConnectStream#21868813::ConnectStream(Buffered 1236 bytes.)
System.Net Information: 0 : [2776] Associating HttpWebRequest#53294272 with ConnectStream#21868813
System.Net Information: 0 : [2776] Associating HttpWebRequest#53294272 with HttpWebResponse#62601592
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#53294272::GetResponse()  -> HttpWebResponse#62601592
System.Net Verbose: 0 : [2776] WebClient#26543418::DownloadString(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] WebClient#26543418::DownloadData(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] WebRequest::Create(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve)
System.Net Verbose: 0 : [2776] HttpWebRequest#37564172::HttpWebRequest(http://www.server.com/mckinley/menu/submit_feedback.asp name=steve&probver=1.0.0.5&ml=Home&comment=steve#314366047)
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#37564172::HttpWebRequest()
System.Net Verbose: 0 : [2776] Exiting WebRequest::Create()  -> HttpWebRequest#37564172
System.Net Verbose: 0 : [2776] HttpWebRequest#37564172::GetResponse()
System.Net Information: 0 : [2776] Associating HttpWebRequest#37564172 with ServicePoint#62718864
System.Net Information: 0 : [2776] Associating Connection#27598869 with HttpWebRequest#37564172
System.Net Verbose: 0 : [0788] HttpWebRequest#37564172::Abort(The operation has timed out)
System.Net Error: 0 : [0788] Exception in the HttpWebRequest#37564172:: - The operation has timed out
System.Net Verbose: 0 : [0788] Exiting HttpWebRequest#37564172::Abort()
System.Net Error: 0 : [2776] Exception in the HttpWebRequest#37564172::EndGetResponse - The operation has timed out
System.Net Verbose: 0 : [2776] HttpWebRequest#37564172::Abort()
System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#37564172::Abort()

 

 

I noticed that there was a difference in the connection header (keep alive), so I have tried forcing the connection to always use keep alive, and that did not solve the problem.  Actually, after doing that, the new trace log did not indicate a keep-alive value for the connection header on the second request (there was no connection header, as these logs show).





Re: .NET Framework Networking and Communication Timeout exception after the first request

Durgaprasad Gorti

I responsed to steve off line but here is the issue.
The code that tests the "connection" or "website" is not closing the response- which means that
the connection is tied up and can;t be reused until the reponse is closed. The next time the feedback button is pressed the WebSite is checked again
the second connection is tied up and there is no more connections to be used since the default connection limit is 2.
So the second time, the feedback request is timing out waiting for a connection to be available.

The fix is to close the response.
Another issue is to not to check for the staus code descriptions like "OK"
the correct way to test for the status codes not descr. Since the Descriptions are not stanard, they are conventional

According to the RFC

The individual values of the numeric status codes defined for HTTP/1.1, and an example set of corresponding Reason-Phrase's, are presented below. The reason phrases listed here are only recommendations -- they MAY be replaced by local equivalents without affecting the protocol.

   Status-Code  =
      "100" ; Section 10.1.1: Continue
     | "101" ; Section 10.1.2: Switching Protocols
     | "200" ; Section 10.2.1: OK