Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Request.BeginGetResponse probem in Windows 7 and unexpected Blocking behaviour

Request.BeginGetResponse probem in Windows 7 and unexpected Blocking behaviour

Scheduled Pinned Locked Moved C#
csharphtmlsysadmin
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vivasaayi
    wrote on last edited by
    #1

    Hello Readers, I am downloading a Html File using the following method.

    public void LoadInitialData()
    {
    HttpWebRequest request = null;

            try
            {
                Stopwatch sw = new Stopwatch();
    
                RaiseStateChangedEvent(10, "Creating Connection to Server");
    
                request = (HttpWebRequest)(WebRequest.Create(this.\_ServerUrl));
    
                RaiseStateChangedEvent(20, "Connected to Server");
    
                int timeout = request.Timeout;
    
                if (request != null)
                {
                    try
                    {
                        sw.Start();
                        request.BeginGetResponse(HttpRequestCallBackMethod, request);
                        sw.Stop();
    
                        RaiseStateChangedEvent(40, "Waiting for Data from Server --Time Taken: " + sw.ElapsedMilliseconds);
                    }
                    catch (Exception ex)
                    {
                        if (onErrorOccured != null)
                            onErrorOccured(ex.Message + "\\n" + ex.StackTrace);
                    }
                }
            }
    
            catch (Exception ex)
            {
                if (onErrorOccured != null)
                    onErrorOccured(ex.Message + "\\n" + ex.StackTrace);
            }
        }
    
        private void HttpRequestCallBackMethod(IAsyncResult iar)
        {
            RaiseStateChangedEvent(70, "Data Received From Server");
    
    
            HttpWebRequest myHttpWebRequest = iar.AsyncState as HttpWebRequest;
            HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(iar);
    
            using (StreamReader input = new StreamReader(response.GetResponseStream()))
            {
                string data = input.ReadToEnd();
                RaiseStateChangedEvent(80, "Processing received Data");
                ProcessData(data);
                RaiseStateChangedEvent(100, "Completed Loading Initial Data");
            }
        }
    
        private void RaiseStateChangedEvent(int progress, string message)
        {
            if (onStateChanged != null)
                onStateChanged.Invoke(progress, message);
        }
    
        private void ProcessData(string data)
        {
            Console.WriteLine("Download Completed - Do any Processing here");
        }
    

    The program is written in .Net 2.0. When I try to run the same in Windows 7, I am not successful. I

    V 1 Reply Last reply
    0
    • V vivasaayi

      Hello Readers, I am downloading a Html File using the following method.

      public void LoadInitialData()
      {
      HttpWebRequest request = null;

              try
              {
                  Stopwatch sw = new Stopwatch();
      
                  RaiseStateChangedEvent(10, "Creating Connection to Server");
      
                  request = (HttpWebRequest)(WebRequest.Create(this.\_ServerUrl));
      
                  RaiseStateChangedEvent(20, "Connected to Server");
      
                  int timeout = request.Timeout;
      
                  if (request != null)
                  {
                      try
                      {
                          sw.Start();
                          request.BeginGetResponse(HttpRequestCallBackMethod, request);
                          sw.Stop();
      
                          RaiseStateChangedEvent(40, "Waiting for Data from Server --Time Taken: " + sw.ElapsedMilliseconds);
                      }
                      catch (Exception ex)
                      {
                          if (onErrorOccured != null)
                              onErrorOccured(ex.Message + "\\n" + ex.StackTrace);
                      }
                  }
              }
      
              catch (Exception ex)
              {
                  if (onErrorOccured != null)
                      onErrorOccured(ex.Message + "\\n" + ex.StackTrace);
              }
          }
      
          private void HttpRequestCallBackMethod(IAsyncResult iar)
          {
              RaiseStateChangedEvent(70, "Data Received From Server");
      
      
              HttpWebRequest myHttpWebRequest = iar.AsyncState as HttpWebRequest;
              HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(iar);
      
              using (StreamReader input = new StreamReader(response.GetResponseStream()))
              {
                  string data = input.ReadToEnd();
                  RaiseStateChangedEvent(80, "Processing received Data");
                  ProcessData(data);
                  RaiseStateChangedEvent(100, "Completed Loading Initial Data");
              }
          }
      
          private void RaiseStateChangedEvent(int progress, string message)
          {
              if (onStateChanged != null)
                  onStateChanged.Invoke(progress, message);
          }
      
          private void ProcessData(string data)
          {
              Console.WriteLine("Download Completed - Do any Processing here");
          }
      

      The program is written in .Net 2.0. When I try to run the same in Windows 7, I am not successful. I

      V Offline
      V Offline
      vivasaayi
      wrote on last edited by
      #2

      Hello Readers, I found the solution for the problem I mentioned. Just add to lines of Code to modify the properties of request object. Delay in the in the initial call is reduced by setting the Proxy as null. This happened in all versions of windows.

      request.Proxy = null;

      Also, In Windows 7, If I send 10 requests, I am only getting 5 or 6 responses. By setting KeepAlive property to false, I am getting all responses.

      request.KeepAlive = true;

      Hope this helps someone! Happy Coding...

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups