How can vi get title and meta description from a url

Discussion in 'C#' started by palme, Nov 3, 2008.

  1. #1
    Hi
    How can vi obtain title and meta keyword and meta description from a url
    with asp without use of any 3rd. component
     
    palme, Nov 3, 2008 IP
  2. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Assuming you mean asp.net, use the httprequest module to retrieve the page, then parse it to retrieve the information you want.
     
    Free Born John, Nov 4, 2008 IP
  3. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can do the same thing in "old" asp. Use XMLHttpRequest
    Google "get url xmlhttp" and you shall recieve
     
    ranabra, Nov 4, 2008 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    The function below will return a string with the complete source code of the requested URL (in asp.net)

    
        Function GetWebPage(ByVal strURI As String) As String
            Dim r As WebResponse
            r = WebRequest.Create(New Uri(strURI)).GetResponse()
            Dim sr As New StreamReader(r.GetResponseStream())
            Do Until sr.EndOfStream
                GetWebPage = sr.ReadToEnd
            Loop
        End Function
    
    Code (markup):
    There is probably an easier way to do this but they way I do it is then use string functions such as Instr() to determine the values of specific tags, e.g for the text within two tags (such as title)

    
        Function GetWebPage(ByVal strURI As String) As String
            Dim r As WebResponse
            r = WebRequest.Create(New Uri(strURI)).GetResponse()
            Dim sr As New StreamReader(r.GetResponseStream())
            Do Until sr.EndOfStream
                GetWebPage = sr.ReadToEnd
            Loop
        End Function
    
        Function getTagText(ByVal strTag As String, ByVal strURL As String) As String
            Dim pos1, pos2 As Integer
            Dim webdoc As String
            Dim HTMLtag, HTMLendtag As String
    
            HTMLtag = "<" & strTag & ">"
            HTMLendtag = "</" & strTag & ">"
    
            webdoc = GetWebPage(strURL)
    
            pos1 = InStr(LCase(webdoc), HTMLtag) + Len(HTMLtag)
            pos2 = InStr(LCase(webdoc), HTMLendtag)
            getTagText = Mid(webdoc, pos1, pos2 - pos1)
        End Function
    
    Code (markup):
    Example usage would be:

    Dim strTitle as String
    strTitle = getTagText ("title", "http://www.yahoo.com")

    This code could be altered to retrieve attributes from within a html tag such as <meta description or <a href etc.
     
    camjohnson95, Nov 5, 2008 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    sorry, you will need to import:

    Imports System.IO
    Imports System.Net
     
    camjohnson95, Nov 5, 2008 IP
  6. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Quality post.
     
    Free Born John, Nov 5, 2008 IP