Results 1 to 9 of 9

Thread: [RESOLVED] Read Web Page using Stream Reader.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Resolved [RESOLVED] Read Web Page using Stream Reader.

    Hi all, after a long time I missed you all....

    From few days I am doing efforts to read web pages using webbrowser control in my desktop application. But it is very slower than my expectations(because I want to read lots of pages in minutes and browser control reads almost one page in 5 to 10 seconds), what I need is to read two or three tags written in web page.

    So finally I decided to use something, which can give me only source code of page. I think StreamReader will help me to read it. I am not sure if it is flaxible with my purpose, or something else is there which can give me only html source code of web page.

    I have written this code to read html code, but it gives error.
    System.ArgumentException: URI formats are not supported.
    Code:
     public static string Navigate(string URL)
            {
                StreamReader SR = new StreamReader(URL);
                return SR.ReadToEnd();
            }
    Please help me to read html tags from web page.

    Thanks a bunch !!
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Read Web Page using Stream Reader.

    URI formats are not supported.
    Did you check the URL. Is that url is opening in any browser ?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Read Web Page using Stream Reader.

    I tried different URLs in this...
    Even I tried http://www.yahoo.com

    same error !!
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Read Web Page using Stream Reader.

    Then you shall go for the System.Net.WebClient.DownloadString Method

    Code:
    Public Shared Sub DownloadString(ByVal address As String)
    
                Dim client As WebClient = New WebClient()
                Dim reply As String = client.DownloadString(address)
    
                Console.WriteLine(reply)
            End Sub
    Please mark you thread resolved using the Thread Tools as shown

  5. #5
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Read Web Page using Stream Reader.

    If you want to use a streamreader you'll need to make a webrequest and read the response stream. DownloadString is much better IMO but if you really want to read it all with StreamReader:

    Code:
    Private Function GetSource(ByVal URL As String) As IO.Stream
         Dim webReq As  Net.WebRequest = Net.WebRequest.Create(URL)
         Dim webResp As Net.WebResponse = webReq.GetResponse
         Return webResp.GetResponseStream
    End Function
    
    '...seperate sub here
    
    Using sRead As New IO.StreamReader(GetSource("http://www.google.com")
          '...
    End Using
    Alternatively if you don't want the flexibility of the direct stream and merely want the whole block of text:

    Code:
    Private Function GetSource(ByVal URL As String) As String
        Dim webReq As Net.WebRequest = Net.WebRequest.Create(URL)
        Dim webResp As Net.WebResponse = webReq.GetResponse
        Using sRead As New IO.StreamReader(webResp.GetResponseStream)
              Return sRead.ReadToEnd
        End Using
    End Function
    If I helped you out, please take the time to rate me

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Read Web Page using Stream Reader.

    Thanx a lot .... danasegarane and J-Deezy.....

    I used webclient... Its amazing.. I was not aware of it... Even its speed is very nice.. I wanted the same thing which can give only the code of page..

    Gr8 !!>..
    Thankx a lot ....
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  7. #7
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] Read Web Page using Stream Reader.

    Oh yeah, you were saying you only wanted a few tags within the document, why not just use the WebBrowser and use

    Code:
    For Each elem As HtmlElement In WebBrowser1.Document.GetElementsByTagName("Tagnamehere")
         '...do something with the element i.e read it's innertext or whatever
    Next
    If I helped you out, please take the time to rate me

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: [RESOLVED] Read Web Page using Stream Reader.

    Web Browser take too much time to load each and every element of page.....
    Some time it does not fire Document_Completed event....
    Why should I load images and videos on page if I don't need them
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  9. #9
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] Read Web Page using Stream Reader.

    Yeah I know that, I'm just saying if you have a large page source and you need to search the whole string for specific parts, it's going to take a while regardless of your method, but a WebBrowser will considerably lessen the amount of hardcoding you'll need to do. Just an opinion. With short sources, downloadstring and Regex is perfect.
    If I helped you out, please take the time to rate me

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width