C# WebResponse Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KyleUbenk
    New Member
    • Oct 2007
    • 4

    C# WebResponse Problem

    Well I have a multi-threaded program which downloads URL sources from the web. Im doing this multiple times at once. The only problem I see is that sometimes it takes a while to download the source
    Code:
    WebResponse ServerResponse = webrequest.GetResponse();
    and only one webresponse can happen at once because the rest of the threads seem to wait on the first webresponse to finish. Is there a way around this? I hope you understand what my problem is.

    Code:
    public static void run() {
    
    string reply = "";
    string link = ("http://www.google.com");
    WebRequest webrequest = WebRequest.Create(link);
    
                try
                {
                    WebResponse ServerResponse = webrequest.GetResponse();
                    StreamReader streamreader = new  StreamReader(ServerResponse.GetResponseStream());
                    reply = streamreader.ReadToEnd();
    
                    ServerResponse.Close();
                    streamreader.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I don't know why it would wait, unless you're using the same object/same thread.

    Have you tried using the HttpWebRequest? It might provide better functionality for you.

    Comment

    • KyleUbenk
      New Member
      • Oct 2007
      • 4

      #3
      Originally posted by Plater
      I don't know why it would wait, unless you're using the same object/same thread.

      Have you tried using the HttpWebRequest? It might provide better functionality for you.
      Could it be something to do with the way im doing the threads? Im new to C# .
      I'm starting the threads similar to this:

      Code:
        
      Thread[] ThreadArray = new Thread[5];
      
                     for (int i = 0; i < 5; i++)
                      {
                        ThreadArray[i] = new Thread(delegate() { runme(URL, timeout); });
                        ThreadArray[i].Start();
                      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Hmm, well that looks ok as far thread starting goes.

        Comment

        Working...