Retrive html content using System.Net.HttpWebRequest

asp.net Romania
  • 10 years ago

    Hello!

    I want to retrive some html content from a given website. For that I use the System.Net.HttpWebRequest:

    Dim connectionString As String = "some url here"

    Try
         
    Dim myRequest As
    System.Net.HttpWebRequest = WebRequest.Create(connectionString)
                myRequest.Credentials = CredentialCache.DefaultCredentials

    '// Get the response

       Dim webResponse As WebResponse = myRequest.GetResponse
       Dim respStream As Stream = webResponse.GetResponseStream

    '//

       Dim ioStream As StreamReader = New StreamReader(respStream)
       Dim pageContent As String = ioStream.ReadToEnd

        Response.Write(pageContent)

    '// Close streams
       
    ioStream.Close()
        respStream.Close()

    Catch ex As Exception
            Me.Label1.Text = ex.Message

    End Try

     

    The above code works, but the problem is that it retrieves all the html content...and I don't nee it all.

    All I need is all the content between <head> and </head>

     

    [ originally posted on forums.asp.net ]

  • 10 years ago

    Just parse the html and take what's between the head tags.

    The nature of the request is that you can't specify that you want just the head tags, because the server always sends the full data when it gets a request.

  • 10 years ago

    Hi pcmattman, and thank you for the reply!

    I thought about what you've said myself, but there is a problem...I don't know how to do it Sad [:(]...

    I've searched the net and I couldn't find an example on how to do it...They were only showing the way of retrieving all the content and not parts of it.

    I'm a seo consultant and a web designer. I always liked VB but my knowledge is limited though...
    (I use programming only when I have to)

    Maybe you can help me providing a sample code, please?

     

    [ I should start learn more...]

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”