Dowload File

R

rony_16

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.StatusCode).

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(100000) 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.Create(d1) as
HttpWebRequest;
webRequest3.KeepAlive = true;
webRequest3.Headers.Add("Keep-Alive", "300");
webRequest3.Headers.Add("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
webRequest3.Headers.Add("Accept-Language",
"en-us,en;q=0.5");
webRequest3.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest3.Headers.Add("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");

webRequest3.CookieContainer = cookies;
HttpWebResponse myResponse3 =
(HttpWebResponse)webRequest3.GetResponse();

Thread.Sleep(10000);
Stream ReceiveStream3 = myResponse3.GetResponseStream();

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

//Downloading ....
//--------------------------------------------------------------------------------------
Console.WriteLine("Connecting to download link...");
HttpWebRequest webRequest2 = WebRequest.Create(re) as
HttpWebRequest;
webRequest2.ContentType =
"application/x-www-form-urlencoded";
webRequest2.KeepAlive = true;
webRequest2.CookieContainer = cookies;
HttpWebResponse myResponse2 =
(HttpWebResponse)webRequest2.GetResponse();
Stream ReceiveStream2 = myResponse2.GetResponseStream();
Console.WriteLine("Downloading...");

Common.SaveStreamToFile(@"C:\scrapingtest\file.csv",
ReceiveStream2);
Console.WriteLine("Done Downloading");

Thanks, Rony
 
J

Joerg Jooss

Thus wrote rony_16,
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.StatusCode).

HTTP is all about exchanging requests and responses. How do you expect the
server to signal a "ready" state? Is there any application specific protocol
layered on top?
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(100000) i do get the full file, but the
file
can be 100Mb and 10sec won't help.

You don't have to wait explicitly. GetResponseStream() will block until either
the entire response has been read or times out.
Are you sure the method Common.SaveStreamToFile() is implemented correctly?

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top