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. Async web request

Async web request

Scheduled Pinned Locked Moved C#
questioncomhelp
2 Posts 2 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.
  • M Offline
    M Offline
    moxol
    wrote on last edited by
    #1

    I have this simple async web request code:

    class AsyncResponse
    {
    public AsyncResponse()
    {
    test2();
    Console.ReadKey();
    }

        public void test2()
        {
      
            for (int i = 0; i < 100; i++)
            {
                string url = "https//www.google.com";
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
            }
            
        }
    
        private void FinishWebRequest(IAsyncResult result)
        {       
                HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
    
                Console.WriteLine("Finished");
        }
    

    }

    I only got on console two "Finished" lines, meaning FinishWebRequest didn't get finished more then 2 times, but I have 100 requests. What is the issue?

    L 1 Reply Last reply
    0
    • M moxol

      I have this simple async web request code:

      class AsyncResponse
      {
      public AsyncResponse()
      {
      test2();
      Console.ReadKey();
      }

          public void test2()
          {
        
              for (int i = 0; i < 100; i++)
              {
                  string url = "https//www.google.com";
      
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                  request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
              }
              
          }
      
          private void FinishWebRequest(IAsyncResult result)
          {       
                  HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
      
                  Console.WriteLine("Finished");
          }
      

      }

      I only got on console two "Finished" lines, meaning FinishWebRequest didn't get finished more then 2 times, but I have 100 requests. What is the issue?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You don't bother checking or timing any of the responses; how do you know what's going on? All this while sitting in your constructor.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      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