Timer Issue

  • Thread starter Thread starter Barry
  • Start date Start date
B

Barry

Hi

I need to download about 100 urls in a WebBrowser control, i want the of
each one to start after one of them has completed eg 2nd to start after 1st
completes etc.

I am parsing the downloaded html in "DownloadComplete" event.

Can someone give me some idea how to do this

TIA
Barry
 
Barry pisze:
Hi

I need to download about 100 urls in a WebBrowser control, i want the of
each one to start after one of them has completed eg 2nd to start after 1st
completes etc.
Why do you nead w WebBrowser. Maybe WebRequest would be good for you if
you don't need to show it.
If i were you I would probably use a background worker to do the job.
 
Forget about the control.

Take control of your code.

Here is a sample to get your started.

It may not be perfect, but it will get you in the right direction.



public void WriteTextFile(string Url, string FilePath, long BufferSize )
{

try
{




//create a web request
HttpWebRequest oHttpWebRequest = null;
oHttpWebRequest = (HttpWebRequest) System.Net.WebRequest.Create(Url);

//set the connection timeout
oHttpWebRequest.Timeout = 60;// m_ConnectTimeout;




//create a response object that we can read a stream from
HttpWebResponse oHttpResponse = (HttpWebResponse)
oHttpWebRequest.GetResponse();


long workingbuffersize = 1;

//if we don't get back anything from the response, throw and exception
if (oHttpResponse == null)
{
throw new Exception("Url is missing or invalid.");
}

StreamReader sr =null;
StreamWriter sw=null;

//Define the encoding type
try
{
//see if the page will give us back an encoding type
if (oHttpResponse.ContentEncoding.Length > 0)
m_enc = Encoding.GetEncoding(oHttpResponse.ContentEncoding);
else
m_enc = Encoding.GetEncoding(1252);
}
catch
{
// *** Invalid encoding passed
m_enc = Encoding.GetEncoding(1252);
}


//create a stream reader grabbing text we get over HTTP
sr = new StreamReader(oHttpResponse.GetResponseStream(),m_enc);

//set the variable that we will use as a buffer to store characters in
while the file is downloading
char[] DownloadedCharChunk = new char[BufferSize];

//go ahead and create our streamwriter to write our file
sw = new StreamWriter(FilePath,false,m_enc);

sw.AutoFlush = false;

//when the working buffer size hits 0 then we know that the file has
finished downloading
while (workingbuffersize > 0)
{
//set the working buffer size based on the length of characters we
receive from the stream
//we will also set DownloadedCharChunk to the set of characters we
recieve from the stream
workingbuffersize = sr.Read(DownloadedCharChunk,0,(int) BufferSize);

if (workingbuffersize > 0)
{
//write DownloadedCharChunk to the file on disk
sw.Write(DownloadedCharChunk,0,(int) workingbuffersize );
}

} // while


//sr.Close(); //moved to finally block
// sw.Close(); //moved to finally block

}

finally
{

if(null!=sr)
{
sr.Close();
}

if(null!=sw)
{
sw.Close();
}

}

}



//--------- and




public MemoryStream GetResultHtmlStream( string Url, long BufferSize )
{


MemoryStream returnMs = null;
StreamReader sr = null;
StreamWriter sw = null;

try
{






//create a web request
HttpWebRequest oHttpWebRequest = null;
oHttpWebRequest = (HttpWebRequest) System.Net.WebRequest.Create(Url);

//set the connection timeout
oHttpWebRequest.Timeout = 60;//m_ConnectTimeout;








//create a response object that we can read a stream from
HttpWebResponse oHttpResponse = (HttpWebResponse)
oHttpWebRequest.GetResponse();

long workingbuffersize = 1;

//if we don't get back anything from the response, throw and exception
if (oHttpResponse == null)
{
throw new Exception("Url is missing or invalid.");
}


try
{
//see if the page will give us back an encoding type
if (oHttpResponse.ContentEncoding.Length > 0)
m_enc = Encoding.GetEncoding(oHttpResponse.ContentEncoding);
else
m_enc = Encoding.GetEncoding(1252);
}
catch
{
// *** Invalid encoding passed
this.m_enc = Encoding.GetEncoding(1252);
}


//create a stream reader grabbing text we get over HTTP
sr = new StreamReader(oHttpResponse.GetResponseStream(),this.m_enc);

//set the variable that we will use as a buffer to store characters in
while the file is downloading
char[] DownloadedCharChunk = new char[BufferSize];

//go ahead and create our streamwriter to write our file
/////StreamWriter sw = new StreamWriter(FilePath,false,enc);

returnMs = new MemoryStream();
StreamWriter sw = new StreamWriter(returnMs);


sw.AutoFlush = false;

//when the working buffer size hits 0 then we know that the file has
finished downloading
while (workingbuffersize > 0)
{
//set the working buffer size based on the length of characters we
receive from the stream
//we will also set DownloadedCharChunk to the set of characters we
recieve from the stream
workingbuffersize = sr.Read(DownloadedCharChunk,0,(int) BufferSize);

if (workingbuffersize > 0)
{
//write DownloadedCharChunk to the file on disk
sw.Write(DownloadedCharChunk,0,(int) workingbuffersize );
}

} // while


sr.Close();
// sw.Close();
//





}
finally
{

if(null!=sr)
{
sr.Close();
}

//not sure about sw here.....it may kill returnMS....

}



return returnMs;


}


}
 
Thanks for the code, i happend to see it today.


sloan said:
Forget about the control.

Take control of your code.

Here is a sample to get your started.

It may not be perfect, but it will get you in the right direction.



public void WriteTextFile(string Url, string FilePath, long BufferSize )
{

try
{




//create a web request
HttpWebRequest oHttpWebRequest = null;
oHttpWebRequest = (HttpWebRequest) System.Net.WebRequest.Create(Url);

//set the connection timeout
oHttpWebRequest.Timeout = 60;// m_ConnectTimeout;




//create a response object that we can read a stream from
HttpWebResponse oHttpResponse = (HttpWebResponse)
oHttpWebRequest.GetResponse();


long workingbuffersize = 1;

//if we don't get back anything from the response, throw and exception
if (oHttpResponse == null)
{
throw new Exception("Url is missing or invalid.");
}

StreamReader sr =null;
StreamWriter sw=null;

//Define the encoding type
try
{
//see if the page will give us back an encoding type
if (oHttpResponse.ContentEncoding.Length > 0)
m_enc = Encoding.GetEncoding(oHttpResponse.ContentEncoding);
else
m_enc = Encoding.GetEncoding(1252);
}
catch
{
// *** Invalid encoding passed
m_enc = Encoding.GetEncoding(1252);
}


//create a stream reader grabbing text we get over HTTP
sr = new StreamReader(oHttpResponse.GetResponseStream(),m_enc);

//set the variable that we will use as a buffer to store characters in
while the file is downloading
char[] DownloadedCharChunk = new char[BufferSize];

//go ahead and create our streamwriter to write our file
sw = new StreamWriter(FilePath,false,m_enc);

sw.AutoFlush = false;

//when the working buffer size hits 0 then we know that the file has
finished downloading
while (workingbuffersize > 0)
{
//set the working buffer size based on the length of characters we
receive from the stream
//we will also set DownloadedCharChunk to the set of characters we
recieve from the stream
workingbuffersize = sr.Read(DownloadedCharChunk,0,(int) BufferSize);

if (workingbuffersize > 0)
{
//write DownloadedCharChunk to the file on disk
sw.Write(DownloadedCharChunk,0,(int) workingbuffersize );
}

} // while


//sr.Close(); //moved to finally block
// sw.Close(); //moved to finally block

}

finally
{

if(null!=sr)
{
sr.Close();
}

if(null!=sw)
{
sw.Close();
}

}

}



//--------- and




public MemoryStream GetResultHtmlStream( string Url, long BufferSize )
{


MemoryStream returnMs = null;
StreamReader sr = null;
StreamWriter sw = null;

try
{






//create a web request
HttpWebRequest oHttpWebRequest = null;
oHttpWebRequest = (HttpWebRequest) System.Net.WebRequest.Create(Url);

//set the connection timeout
oHttpWebRequest.Timeout = 60;//m_ConnectTimeout;








//create a response object that we can read a stream from
HttpWebResponse oHttpResponse = (HttpWebResponse)
oHttpWebRequest.GetResponse();

long workingbuffersize = 1;

//if we don't get back anything from the response, throw and exception
if (oHttpResponse == null)
{
throw new Exception("Url is missing or invalid.");
}


try
{
//see if the page will give us back an encoding type
if (oHttpResponse.ContentEncoding.Length > 0)
m_enc = Encoding.GetEncoding(oHttpResponse.ContentEncoding);
else
m_enc = Encoding.GetEncoding(1252);
}
catch
{
// *** Invalid encoding passed
this.m_enc = Encoding.GetEncoding(1252);
}


//create a stream reader grabbing text we get over HTTP
sr = new StreamReader(oHttpResponse.GetResponseStream(),this.m_enc);

//set the variable that we will use as a buffer to store characters in
while the file is downloading
char[] DownloadedCharChunk = new char[BufferSize];

//go ahead and create our streamwriter to write our file
/////StreamWriter sw = new StreamWriter(FilePath,false,enc);

returnMs = new MemoryStream();
StreamWriter sw = new StreamWriter(returnMs);


sw.AutoFlush = false;

//when the working buffer size hits 0 then we know that the file has
finished downloading
while (workingbuffersize > 0)
{
//set the working buffer size based on the length of characters we
receive from the stream
//we will also set DownloadedCharChunk to the set of characters we
recieve from the stream
workingbuffersize = sr.Read(DownloadedCharChunk,0,(int) BufferSize);

if (workingbuffersize > 0)
{
//write DownloadedCharChunk to the file on disk
sw.Write(DownloadedCharChunk,0,(int) workingbuffersize );
}

} // while


sr.Close();
// sw.Close();
//





}
finally
{

if(null!=sr)
{
sr.Close();
}

//not sure about sw here.....it may kill returnMS....

}



return returnMs;


}


}











Barry said:
Hi

I need to download about 100 urls in a WebBrowser control, i want the of
each one to start after one of them has completed eg 2nd to start after
1st completes etc.

I am parsing the downloaded html in "DownloadComplete" event.

Can someone give me some idea how to do this

TIA
Barry
 
Back
Top