none
HttpWebResponse works on the first time but never on the second or so on trys after the application is opened RRS feed

  • Question

  • So earlier this week I had a event/method with logic to retrieve directory listing from our secure server for users.

                string dirPath = @"/";
                string fullPath = urlLink + dirPath;
                Uri uri = new Uri(fullPath);
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.CreateDefault(uri);
                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string html = reader.ReadToEnd();
    
                            int i = 0;
                            while ((i = html.IndexOf("<A HREF=", i)) != -1)
                            {
                                int j = html.IndexOf("\">", i);
    
                                if (!html.Substring(i, j - i).Replace("<A HREF=\"", "").Contains(".config"))
                                {
                                    files.Add(html.Substring(i, j - i).Replace("<A HREF=\"", ""));
                                    i = j;
                                }
                                else
                                {
                                    i = j;
                                }
                            }
                        }
                    }
    
                    foreach (string item in files)
                    {
                        chkFiles.Items.Add(item);
                    }
                }

    So today we have run into a problem where the above will run the first time the application is run, any additional times we open and run the application, it doesnt work. Here is the catch, when it does fail to work, after the response time out, we can try again and it works..

    We even tried to set the request.timeout parameter to something besides the default and it always uses the default 100 seconds.

    What could be wrong? Is there a way to clean up the request calls? In case there are some that aren't being cleaned up.

    We know the site works, as we can access and view it view browser anytime without delay or issues.

    Wednesday, August 26, 2015 10:37 PM

All replies

  • Hi Cubangt,

    According to your description, in my opinion, it is may be the server side setting a

    receiveTimeout as 100 seconds.

    And you said you can't open and run the application, I guess there has a process of  the

    communicationchannel is always activity. So, I suggest you when you close the client you

    need close the communication channel first.

    For more information, please refer to the following articles:

    1.Avoiding Problems with the Using Statement

    I hope that will be helpful to you.

    Best Regards,

    Grady


    Thursday, August 27, 2015 5:51 AM
    Moderator
  • May have been little unclear, I can open the program all day long, but when attempting to execute the event is  doesn't work after the first attempt. What I have attached is what is within that event, nothing else.. NOW because we populate a checkboxlist with the results so that the user can make selection to download, we also have similar code in the "doubleclick"event of the checkboxlist control. That works 100% of the time and is calling the same paths as the first event is.. In the provided link its showing to close the "client" in its examples, but what would the "client" be in my example code?

    Would it be the request? response? or stream?

    Because we already tried to dispose of the request, but that doesn't have a "dispose" property. Response and stream do, but isn't that the job of the "using" statement?

    Thursday, August 27, 2015 2:27 PM
  • Also if the server is doing the timeout at 100 sec, where can I check or change that?

    I have found where and changed it for this testing to 20 seconds and still having issues..

    I even changed the code to use WebClient looking like this now

    using (WebClient client = new WebClient())
    {
         html = client.DownloadString(fullPath);
    }

    And still running into issues. I have even rebooted my pc and stopped and started the site within IIS as well as stopping and starting the application pool for the site and still have issues..

    There has to be some explanation as to why this has worked great up til this past Monday and now having all these problems..

    • Edited by Cubangt Thursday, August 27, 2015 3:48 PM
    Thursday, August 27, 2015 2:46 PM
  • Also realized that I didn't mention that all of this is running the application in debug within VS.

    I just ran the application on our mac mini(its a small app we built running it using mono on the mac) it works like a charm from beginning to end, so I know that the code is right and working, so why the problems within VS and debugging it..


    Also ran the exe generated from the project/solution and it works.. what within VS could be causing me these problems testing/debugging?
    • Edited by Cubangt Thursday, August 27, 2015 4:03 PM
    Thursday, August 27, 2015 3:59 PM
  • Also realized that I didn't mention that all of this is running the application in debug within VS.

    I just ran the application on our mac mini(its a small app we built running it using mono on the mac) it works like a charm from beginning to end, so I know that the code is right and working, so why the problems within VS and debugging it..


    Also ran the exe generated from the project/solution and it works.. what within VS could be causing me these problems testing/debugging?
    You wait or take too much time on the WCF service side it debug mode, the service is going to timeout. You have to  do something in debug mode and have it return to the client-side before it times out or adjust the timeout time the service is using so that you can stay in the WCF service code for longer periods of time without the WCF service timing out. It doesn't matter if is a ASP.NET WCF or non ASP.NET Web WCF service. You only have so much time in debug mode on the WCF service side,  and it is based on the timeout setting set on the client-side, if you are not aware of this. The default timeout is 1 minute.
    Thursday, August 27, 2015 8:57 PM
  • ok, guess let me clear it up some more, when I say "debug" I have the project open in VS, I hit "Start" to begin the debugging (NO BREAKPOINTS) set during the testing.. so that im not preventing anything from happening. BUT it always times out on the response line.

    Thursday, August 27, 2015 9:32 PM
  • ok, guess let me clear it up some more, when I say "debug" I have the project open in VS, I hit "Start" to begin the debugging (NO BREAKPOINTS) set during the testing.. so that im not preventing anything from happening. BUT it always times out on the response line.

    I don't know what you are doing or what the code is doing, but it is taking too long for it to make the round trip,  and it is timing out.

    Did you try it without running in debug mode in VS, which you can do too?

    Thursday, August 27, 2015 9:41 PM
  • Well that's the problem im having, all im doing it chosing to "start" the application in debug mode within VS and let it do its thing and it always times out.. but if I go directly to the exe file that is generated and don't use VS, it runs and works.. something within VS is no longer allowing me to run the application in debug without the time out issue.
    Thursday, August 27, 2015 9:49 PM
  • Well that's the problem im having, all im doing it chosing to "start" the application in debug mode within VS and let it do its thing and it always times out.. but if I go directly to the exe file that is generated and don't use VS, it runs and works.. something within VS is no longer allowing me to run the application in debug without the time out issue.

    Did you try it without running in debug mode in VS, which you can do too?

    You didn't answer the question.


    Thursday, August 27, 2015 10:59 PM
  • If you are referring to changing the drop down from debug to release yes, tried that and still same issues, no improvement. :(

    Friday, August 28, 2015 1:52 AM
  • If you are referring to changing the drop down from debug to release yes, tried that and still same issues, no improvement. :(


    No. I mean go to the Debug tab in VS and select option to run Without Debugger in VS and run the solution. :)
    Friday, August 28, 2015 2:00 AM
  • ok, didn't know that, I will try that when I open VS again.

    thanks..Now if that works or helps what does that do for me? Will that point to something as a potential issue? Code issue? or VS issue?

    Friday, August 28, 2015 2:05 AM