Home All Groups Group Topic Archive Search About
Author
13 Sep 2007 2:08 PM
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

Author
2 Oct 2007 9:47 PM
sloan
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;


  }


  }











Show quote
"Barry" <some***@nowhere.com> wrote in message
news:OgpGS%23g9HHA.4784@TK2MSFTNGP05.phx.gbl...
> 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
>
Author
18 Oct 2007 2:26 PM
Barry
Thanks for the code, i happend to see it today.


Show quote
"sloan" <sl***@ipass.net> wrote in message
news:%23oPaE3TBIHA.3780@TK2MSFTNGP05.phx.gbl...
>
> 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" <some***@nowhere.com> wrote in message
> news:OgpGS%23g9HHA.4784@TK2MSFTNGP05.phx.gbl...
>> 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
>>
>
>

AddThis Social Bookmark Button