Dowload File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rony_16

    Dowload File

    Hi,
    I have problem with downloading a file using WebRequest and
    HttpWebResponse .
    Inorder to download this file i need to call to url that tells to
    prepare the file for download,
    and then i call to another url that downloads the file.
    I have a problem with that because:

    1. i do not know when the server finished the preparetion.
    2.i checked the HttpStatusCode but he doen't changes
    (myresponse.Sta tusCode).

    the problem causes the application to download only a part of the file
    and that because the server hasn't prepared the file yet.
    if i give him Thread.Sleep(10 0000) i do get the full file, but the file
    can be 100Mb and 10sec won't help.

    the code is:
    //Preparing the file todownload...
    //---------------------------------------------------------------------------------------------------------
    HttpWebRequest webRequest3 = WebRequest.Crea te(d1) as
    HttpWebRequest;
    webRequest3.Kee pAlive = true;
    webRequest3.Hea ders.Add("Keep-Alive", "300");
    webRequest3.Hea ders.Add("Accep t","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,ima ge/png,*/*;q=0.5");
    webRequest3.Hea ders.Add("Accep t-Language",
    "en-us,en;q=0.5");
    webRequest3.Hea ders.Add("Accep t-Encoding", "gzip,deflate") ;
    webRequest3.Hea ders.Add("Accep t-Charset",
    "ISO-8859-1,utf-8;q=0.7,*;q=0.7 ");

    webRequest3.Coo kieContainer = cookies;
    HttpWebResponse myResponse3 =
    (HttpWebRespons e)webRequest3.G etResponse();

    Thread.Sleep(10 000);
    Stream ReceiveStream3 = myResponse3.Get ResponseStream( );

    //-----------------------------------------------------------------------------------

    //Downloading ....
    //--------------------------------------------------------------------------------------
    Console.WriteLi ne("Connecting to download link...");
    HttpWebRequest webRequest2 = WebRequest.Crea te(re) as
    HttpWebRequest;
    webRequest2.Con tentType =
    "applicatio n/x-www-form-urlencoded";
    webRequest2.Kee pAlive = true;
    webRequest2.Coo kieContainer = cookies;
    HttpWebResponse myResponse2 =
    (HttpWebRespons e)webRequest2.G etResponse();
    Stream ReceiveStream2 = myResponse2.Get ResponseStream( );
    Console.WriteLi ne("Downloading ...");

    Common.SaveStre amToFile(@"C:\s crapingtest\fil e.csv",
    ReceiveStream2) ;
    Console.WriteLi ne("Done Downloading");

    Thanks, Rony

  • Kevin Spencer

    #2
    Re: Dowload File

    Asked and answered on the *other* newsgroup you posted this exact same
    question to.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Digital Carpenter

    A man, a plan, a canal,
    a palindrome that has gone to s**t.

    "rony_16" <rony.vainblat@ gmail.comwrote in message
    news:1158674436 .775121.232090@ m73g2000cwd.goo glegroups.com.. .
    Hi,
    I have problem with downloading a file using WebRequest and
    HttpWebResponse .
    Inorder to download this file i need to call to url that tells to
    prepare the file for download,
    and then i call to another url that downloads the file.
    I have a problem with that because:
    >
    1. i do not know when the server finished the preparetion.
    2.i checked the HttpStatusCode but he doen't changes
    (myresponse.Sta tusCode).
    >
    the problem causes the application to download only a part of the file
    and that because the server hasn't prepared the file yet.
    if i give him Thread.Sleep(10 0000) i do get the full file, but the file
    can be 100Mb and 10sec won't help.
    >
    the code is:
    //Preparing the file todownload...
    //---------------------------------------------------------------------------------------------------------
    HttpWebRequest webRequest3 = WebRequest.Crea te(d1) as
    HttpWebRequest;
    webRequest3.Kee pAlive = true;
    webRequest3.Hea ders.Add("Keep-Alive", "300");
    webRequest3.Hea ders.Add("Accep t","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,ima ge/png,*/*;q=0.5");
    webRequest3.Hea ders.Add("Accep t-Language",
    "en-us,en;q=0.5");
    webRequest3.Hea ders.Add("Accep t-Encoding", "gzip,deflate") ;
    webRequest3.Hea ders.Add("Accep t-Charset",
    "ISO-8859-1,utf-8;q=0.7,*;q=0.7 ");
    >
    webRequest3.Coo kieContainer = cookies;
    HttpWebResponse myResponse3 =
    (HttpWebRespons e)webRequest3.G etResponse();
    >
    Thread.Sleep(10 000);
    Stream ReceiveStream3 = myResponse3.Get ResponseStream( );
    >
    //-----------------------------------------------------------------------------------
    >
    //Downloading ....
    //--------------------------------------------------------------------------------------
    Console.WriteLi ne("Connecting to download link...");
    HttpWebRequest webRequest2 = WebRequest.Crea te(re) as
    HttpWebRequest;
    webRequest2.Con tentType =
    "applicatio n/x-www-form-urlencoded";
    webRequest2.Kee pAlive = true;
    webRequest2.Coo kieContainer = cookies;
    HttpWebResponse myResponse2 =
    (HttpWebRespons e)webRequest2.G etResponse();
    Stream ReceiveStream2 = myResponse2.Get ResponseStream( );
    Console.WriteLi ne("Downloading ...");
    >
    Common.SaveStre amToFile(@"C:\s crapingtest\fil e.csv",
    ReceiveStream2) ;
    Console.WriteLi ne("Done Downloading");
    >
    Thanks, Rony
    >

    Comment

    Working...