Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Timer Issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barry - 13 Sep 2007 15:08 GMT
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
Doker - 13 Sep 2007 18:07 GMT
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.
sloan - 02 Oct 2007 22:47 GMT
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;

 }

 }

> Hi
>
[quoted text clipped - 8 lines]
> TIA
> Barry
Barry - 18 Oct 2007 15:26 GMT
Thanks for the code, i happend to see it today.

> Forget about the control.
>
[quoted text clipped - 212 lines]
>> TIA
>> Barry

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.