简体   繁体   中英

Checking if file exists on web server and get date/time and size

How can I check if file exists on a web server (that needs user/password to access) and how can I get its size and date/time without download it before?

I think that is necessary the use of HttpWebRequest.GetResponse but it's not very familiar to me.

I am using VB.NET in Visual Studio 2008.

You can send a HTTP HEAD request to just get the headers for the file you intend to download. Following is a sample request:

HEAD /wikipedia/commons/6/62/Abhi_2014.jpg HTTP/1.1
Host: upload.wikimedia.org

In VB.NET, make sure that the "Method" property of the HttpWebRequest object is "HEAD".

request.Method = "HEAD"
request.GetResponse()

My solution:

Private Sub ObtainFSD()
Dim filsize As Long
Dim lastmodi As DateTime
Dim request As System.Net.WebRequest
request = Net.WebRequest.Create("http://10.132.1.29/JDOWN/teleini.txt")
request.Method = "HEAD"
Dim response = request.GetResponse()
filsize = response.contentLength
lastmodi = response.lastModified
debug.Writeline (filsize)
debug.Writeline (lastmodi)
response.close()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM