Vector
Vector

Reputation: 3235

How do I get last modified date of website

I have website URL's in a DataGridView Cell when I click on the Cell the website is loaded in Chrome. I am using VS 2019 and VB.Net only I do not have ASP.Net installed.
I have tried a bunch of different concepts from some SO post that go back to 2011
With very little success I found one function that looked workable but no results I will post that code.
My question is How do I get last modified date of website?
If using VB.Net ONLY is not workable point me to a reference for other tools needed.

Public Property DateTime_LastModified As String
Dim webPAGE As String

This code is inside a click event for the DataGridView

        ElseIf gvTxType = "View" Then
        webPAGE = row.Cells(2).Value.ToString()
        'Modified: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        Process.Start(webPAGE)
        GetDateTimeLastModified(requestUriString)
        Label1.Text = DateTime_LastModified
        'Dim strPath As String = webPAGE + "javascript : alert(document.lastModified)"
        'Dim strPath As String = Request.PhysicalPath
        'Server.MapPath
        'Label1.Text = System.IO.File.GetLastWriteTime(webPAGE).ToString()
        'Label1.Text = strPath '"Modified: " + System.Web.UI.GetLastWriteTime(strPath).ToString()
        'Label1.Text = strPath + "Modified:" + System.MapPath.Request.ServerVariables.Get("SCRIPT_NAME")
        'Process.Start(webPAGE)

Here is the Edit I tried from the Answer

Public Class GetURLdate1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim strURL As String
        strURL = TextBox1.Text

        Dim client = New HttpClient()
        Dim msg As New HttpRequestMessage(HttpMethod.Head, strURL)
        Dim resp = client.SendAsync(msg).Result
        Dim strLastMod As String = resp.Content.Headers.LastModified.ToString

        MsgBox("Last mod as string date" & vbCrLf & strLastMod)

    'Dim lastMod As DateTime = CDate(strLastMod)

    'MsgBox(lastMod)

End Sub

Private Sub GetURLdate1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    TextBox1.Text = "https://www.youtube.com/c/stevinmarin/videos"
    '"https://stackoverflow.com/questions/70825821/how-do-i-get-last-modified-date-of-website"

End Sub

Returns NO Value for strLastMod

Upvotes: 0

Views: 838

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 48954

Ok, so it not clear if you need a routine to go get all the URL's in the grid, and update the last updated for each site/url?

Or are you looking to ONLY update the site WHEN you click on the button to jump to the site?

It is easy to do both.

I mean, say we have this markup - drop in a gridview (I let the wizards created it).

I then blow out the datasoruce control, and then remove the data soruce ID setting from the GV

So, I have this markup:

    <div style="padding:35px">
        <asp:GridView ID="GridView1" runat="server" CssClass="table" Width="65%"
            AutoGenerateColumns="False" DataKeyNames="ID" >
            <Columns>
                <asp:BoundField DataField="Url" HeaderText="Url" ItemStyle-Width="500" />
                <asp:BoundField DataField="LastUpDated" HeaderText="Last UpDated"  />
                <asp:BoundField DataField="LastVisit" HeaderText="Last Visit"  />
                <asp:TemplateField HeaderText="View" ItemStyle-HorizontalAlign="Center" >
                    <ItemTemplate>
                        <asp:Button ID="cmdJump" runat="server" Text="View" CssClass="btn"
                            OnClick="cmdJump_Click"
                            />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <asp:Button ID="cmdGetAll" runat="server" Text="Upate all Last updated" CssClass="btn" />
    </div>

Ok, and my code to load the gv is this:

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid()
    End If
End Sub

Sub LoadGrid()

    Using conn = New SqlConnection(My.Settings.TEST4)

        Using cmdSQL = New SqlCommand("SELECT * FROM tblSites", conn)

            Dim rstData As New DataTable
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rstData
            GridView1.DataBind()

        End Using

    End Using

End Sub

And now we have this:

enter image description here

Ok, so just dropped in a plane jane button into the GV. When I click on that button, I will update the last visit - not clear if you ALSO want this to update the last update for the given url?

we can do both.

So, out simple button we dropped? Well, lets have it update the last time we visit (click) to jump to the site).

Protected Sub cmdJump_Click(sender As Object, e As EventArgs)

    Dim cmdView As Button = sender
    Dim gRow As GridViewRow = cmdView.NamingContainer
    Dim PKID As Integer = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    ' udpate with last visit click
    Using conn = New SqlConnection(My.Settings.TEST4)
        Using cmdSQL = New SqlCommand("update tblSites SET LastVisit = @Visit WHERE ID = @ID", conn)

            conn.Open()
            cmdSQL.Parameters.Add("@Visit", SqlDbType.DateTime).Value = Date.Now
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = PKID
            cmdSQL.ExecuteNonQuery()

        End Using
    End Using

    ' Now jump to that url
    Response.Redirect(gRow.Cells(0).Text)

End Sub

And the button to go fetch and update all last updates (every row) of the given URL's cna be this:

Protected Sub cmdGetAll_Click(sender As Object, e As EventArgs) Handles cmdGetAll.Click

    Using conn = New SqlConnection(My.Settings.TEST4)

        Using cmdSQL = New SqlCommand("SELECT * FROM tblSites", conn)

            Dim rstData As New DataTable
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)

            For Each OneRow As DataRow In rstData.Rows

                Dim client = New HttpClient()
                Dim msg As New HttpRequestMessage(HttpMethod.Head, OneRow("Url").ToString)
                Dim resp = client.SendAsync(msg).Result
                Dim lastMod As DateTimeOffset? = resp.Content.Headers.LastModified
                OneRow("LastUpDated") = lastMod.Value.ToString
            Next

            Dim da As New SqlDataAdapter(cmdSQL)
            Dim daU As New SqlCommandBuilder(da)
            da.Update(rstData)

            ' now re-load grid
            LoadGrid()

        End Using

    End Using

End Sub

So, the above has the code to update the data for all URL's.

But, your question seems to suggest that when you click on the GV row button, you ALSO want to get/save/update the given row of the GV with the last update information from the web site url?

Ok, then, we modify our click code, to update both our last click visit, and then also get that web site last update information.

So, just change the button click row code to this then:

Protected Sub cmdJump_Click(sender As Object, e As EventArgs)

    Dim cmdView As Button = sender
    Dim gRow As GridViewRow = cmdView.NamingContainer
    Dim PKID As Integer = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    Dim client = New HttpClient()
    Dim msg As New HttpRequestMessage(HttpMethod.Head, gRow.Cells(0).Text)
    Dim resp = client.SendAsync(msg).Result
    Dim lastMod As DateTime = resp.Content.Headers.LastModified.ToString

    ' udpate with last visit click, and also get laste update from web site
    Using conn = New SqlConnection(My.Settings.TEST4)

        Dim strSQL As String =
            "update tblSites SET LastVisit = @Visit,LastUpdated = @LastUpDate WHERE ID = @ID"
        Using cmdSQL = New SqlCommand(strSQL, conn)

            conn.Open()
            cmdSQL.Parameters.Add("@Visit", SqlDbType.DateTime).Value = Date.Now
            cmdSQL.Parameters.Add("@LastUpDate", SqlDbType.DateTime).Value = lastMod
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = PKID
            cmdSQL.ExecuteNonQuery()

        End Using
    End Using

    ' Now jump to that url
    Response.Redirect(gRow.Cells(0).Text)

End Sub

Edit: Not using asp.net

I now see in your post that you suggest you are not using asp.net. However, the above code would work near the same if you are say using a vb.net desktop + gridview. In other words, the code should be similar, and that includes the code to get that web site date.

so, you need to use follow hyper link, but the code to get the date from the web site, and even your general form layout for desktop. The results - even the way your gird looks will thus be very similar to the above web page, the overall idea here thus applies equal well to desktop or web based. I mean, if you have visual studio installed, then you could try above - as VS does install the web bits and parts for you.

Edit #2 - code to get last modified date.

There is NOT some process start, NEVER suggested to use as such. The code you need is thus this:

    webPAGE = row.Cells(2).Value.ToString()
    Dim client = New HttpClient()
    Dim msg As New HttpRequestMessage(HttpMethod.Head,webPAGE)
    Dim resp = client.SendAsync(msg).Result
    Dim lastMod As DateTime = resp.Content.Headers.LastModified.ToString

    MsgBox("DATE " & lastMod)

Edit#3: Code as win forms

Ok, so we create a blank new windows form.

Drop in a text box, drop in a button.

We have this code:

Imports System.Net.Http

Public Class GetURLdate1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim strURL As String
        strURL = TextBox1.Text

        Dim client = New HttpClient()
        Dim msg As New HttpRequestMessage(HttpMethod.Head, strURL)
        Dim resp = client.SendAsync(msg).Result
        Dim strLastMod As String = resp.Content.Headers.LastModified.ToString

        MsgBox("Last mod as string date" & vbCrLf & strLastMod)

        Dim lastMod As DateTime = strLastMod

        MsgBox(lastMod)

    End Sub

    Private Sub GetURLdate1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        TextBox1.Text =
            "https://stackoverflow.com/questions/70825821/how-do-i-get-last-modified-date-of-website"

    End Sub

So, I used THIS web page for the url.

When I run this form - click on button, I see this:

enter image description here

Upvotes: 1

Related Questions