【问题标题】:How can I fetch the last-modified value of a remote file?如何获取远程文件的最后修改值?
【发布时间】:2014-07-14 18:21:22
【问题描述】:

我想知道远程文件的最后修改日期(通过 url 定义)。
并且只下载它,如果它比我本地存储的更新。

我设法对本地文件执行此操作,但找不到针对远程文件执行此操作的解决方案(无需下载)

工作:

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

不工作:

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

我很想有一个解决方案,只需要下载文件的标题

【问题讨论】:

    标签: vb.net url last-modified


    【解决方案1】:

    您可以使用System.Net.Http.HttpClient 类从服务器获取上次修改日期。因为它正在发送一个HEAD 请求,所以它不会获取文件内容:

    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

    【讨论】:

    • 你能想出一个不需要 azure 的解决方案吗? (现在安装它,然后测试您的解决方案)
    • @Wurstbro 我不知道为什么这需要 Azure - 只是对 System.Net.Http.dll 的引用,它包含在 .NET 4.5 或 Microsoft.Net.Http NuGet 包中如果目标是 .NET 4.0。你看到什么需要 Azure - 我想我已经安装了 SDK,所以我可能会在没有意识到的情况下引用一些东西。
    • 好吧,我的环境中不存在 System.Net.Http。我有 HttpListener 等,但没有空白 Http
    • @Wurstbro 您的项目的目标是什么版本的 .NET?
    • @Wurstbro 您已将 System.Net.Http.dll 添加到项目引用中?
    【解决方案2】:

    如果服务器提供,您可以通过HTTP header Last-Modified property 获取。但是您仍然无法下载完整的文件。

    您可以通过 FTP 获取它。
    查看服务器是否允许您查看文件夹中的文件列表。
    如果网站在某个地方提供日期,您可以通过屏幕报废来获取。

    【讨论】:

    • 马克的解决方案更好,但还是谢谢你
    【解决方案3】:

    我知道这是一个有点老的问题,但仍有更好的答案。

                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
    

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多