Using System.Net Class

i want to write a program which will download a file from internet.......but during that i want to show some updates of most likely like progress or so...... i came to know that i have to follow the IAsync...but no further could you please provide some useful info.


Answer this question

Using System.Net Class

  • Mach1

    Dotnet Framework: 1.1 (VS2003.NET)


  • Denis Brasfield

    there is a little problem in the web reponse i have created....on a slow internet connection is giving a time out error while internet is connect.....what value of timeout is sufficient.

  • srivastava

    right! thanks for the idea. but could you please elaborate some thing else for me that how can i use iasync calls for webresponse

  • JimSmith

    The new System.NEt WebClient has APIs for you to subscribe to the events of progress.
    The following code should provide some guidance.
    Let me know if you have more questions



    #region Using directives

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Net;
    using System.Net.Cache;
    using System.Drawing;
    using System.Windows.Forms;

    #endregion

    namespace WebClientTest
    {
     partial class frmWebClient : Form
     {
      WebClient wc = new WebClient();
      public frmWebClient()
      {
       InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
       wc.DownloadStringAsync(new Uri("/listener/default.htm">http://<yourmachinename>/listener/default.htm"));
       wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
       wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
      }

      void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
      {
       this.progressBar1.Value = e.ProgressPercentage;
       this.label1.Text = e.ProgressPercentage.ToString() + "% Completed";

      }

      void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
      {
       this.webBrowser1.DocumentText = e.Result;
      }
     }
    }

     



  • Belsen

    The timeout value choose is going to depend on several things...

    1. What server you are making the request to.  Is the server frequently overloaded   What is the average response time for the server   What is the longest response time for the server   Do you trust the server to correctly respond within a reasonable time  
      1. One possible timeout is the longest known response time from that server.
      2. You could try something like 2 x Average_Response_Time
    2. What is the desired user experience   Is there a GUI that is waiting for the response to come back   If so, how long do you dare make the user wait before reporting giving up and reporting an error
    3. You should also take into account that an HttpWebRequest actually has two types of timeouts (second one applies only to v1.1+ for the .Net Framework)
      1. HttpWebRequest.Timeout - This is the time it takes for the initial request to be sent to the server plus the time it takes for the server to respond with a status code and headers (does not include the body of the response).
      2. HttpWebRequest.ReadWriteTimeOut - This applies to any single read/write operation on the underlying socket.  The server could send back the response headers and then hang during while sending the response body.  Setting this property allows you to timeout when the server just stops sending the body of the response. 


  • Sunil_D

    Well in the most basic terms this is what is going on
    1) When you call WebRequest's async methods, the socket that is associated with that request is put in a completion port with pending read or write

    2) one or more of the threads in the thread pool are waiting to
    get notified of the completion port.

    3) When the socket has data available to read or write, a thread in the thread pool wakes up and processes the read/write, then once it is done calls a callback.

    Let me know if you would like to see a sample
    In fact
    http://blogs.msdn.com/feroze_daud/archive/2004/10/07/239562.aspx
    is a very good start

  • TuscoGrocers

    i have tried the code you provided but some problems are there....
    like System.Net.Cache is not available with System.Collections.Generic and the methods you callled wc.DownloadStringAsy are also not available Tongue Tied

  • Prasadi de Silva

    thanks a really useful link.
    i am studying it will ask you if require further assistance.

  • Sheldon Penner

    What version of the framework are you using

  • hytechpro

    Depends on your network and the resonse length.
    Try to increase until it succeeds. Then experiment with what could
    be a reasonable value

  • Michael McCarthy

    Unfortunately, in 1.1 there is no friendly API you could use.
    One thing you could do is to look at the Content-Length header of the response.
    Then as you read the resonse you can compute the % and update a progress bar.
    Thats what we do (in simple terms) for whidbey

  • Using System.Net Class