How to get the last modified value of a deleted file?

I would like to know the last modified date of the deleted file (defined via URL).
And download it only if it is newer than my locally stored one.

I managed to do this for local files, but cannot find a solution for this for deleted files (without downloading them)

works:

Dim infoReader As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("C:/test.txt")
MsgBox("File was last modified on " & infoReader.LastWriteTime)  

does not work:

        Dim infoReader As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("http://google.com/robots.txt")
        MsgBox("File was last modified on " & infoReader.LastWriteTime)  

I would like to have a solution that should only download file headers

+4
source share
3 answers

You can use the class System.Net.Http.HttpClientto get the last modified date from the server. Since it sends a request HEAD, it will not receive the contents of the file:

Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Head, "http://google.com/robots.txt")
Dim resp = client.SendAsync(msg).Result
Dim lastMod = resp.Content.Headers.LastModified

If-Modified-Since GET. , 304 - Not Modified, ( ) 200 - OK, ( ), .

Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Get, "http://google.com/robots.txt")
msg.Headers.IfModifiedSince = DateTimeOffset.UtcNow.AddDays(-1) ' use the date of your copy of the file
Dim resp = client.SendAsync(msg).Result
Select Case resp.StatusCode
    Case HttpStatusCode.NotModified
        ' Your copy is up-to-date
    Case HttpStatusCode.OK
        ' Your copy is out of date, so save it
        File.WriteAllBytes("C:\robots.txt", resp.Content.ReadAsByteArrayAsync.Result)
End Select

.Result, - await.

+2

, HTTP- Last-Modified. .

FTP.
, .
- -, .

+1

I know this is a little old question, but there is an even better answer.

            Dim req As WebRequest = HttpWebRequest.Create("someurl")
            req.Method = "HEAD"
            Dim resp As WebResponse = req.GetResponse()
            Dim remoteFileLastModified As String = resp.Headers.Get("Last-Modified")
            Dim remoteFileLastModifiedDateTime As DateTime
            If DateTime.TryParse(remoteFileLastModified, remoteFileLastModifiedDateTime) Then
                MsgBox("Date Last Modified:" + remoteFileLastModifiedDateTime.ToString("d MMMM yyyy dddd HH:mm:ss"))
            Else
                MsgBox("could not determine")
            End If
+1
source

Source: https://habr.com/ru/post/1548382/


All Articles