My company recently moved our FTP site to a secure WebDAV server (Web-based Distributed Authoring and Versioning). I was tasked with developing some automated download software. I thought there would be plenty of examples on the internet of how to do this, but was surprised to realize the difficulty I was having finding enough information. Because it took me a while to piece it together, I thought I would share my solution with others who may have similar needs.
Although there may be different ways for accessing and downloading information from a WebDAV server, I chose to use HTTPS, and have built this demo around that.
If you are familiar with
HttpWebResponse, then you should feel right at home. The only difference between a regular server request and a WebDAV server request is that "Translate: f" header must be added, along with setting
SendChunks = True.
If you're new to downloading files from a WebDAV server, then just follow along. I've included a demo that you can download and walk through to see how it works. The demo was created with VS 2008, Visual Basic, .NET Framework 2.0.
Using the Code
This first thing we can do is combine our URL and port, if a port was provided:
Dim url As String = "https://someSecureTransferSite.com/fileToDownload.dat"
Dim port As String = "443"
If port <> "" Then
Dim u As New Uri(url)
Dim host As String = u.Host
url = url.Replace(host, host & ":" & port)
Next, we can request the file from the WebDAV server.
Dim userName As String = "UserName"
Dim password As String = "Password"
Dim request As HttpWebRequest = _
request.Credentials = New NetworkCredential(userName, password)
request.Method = WebRequestMethods.Http.Get
request.SendChunked = True
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
After the server has given us a response, we can begin to download the file.
Dim destination As String = "c:\temp\downloadedFile.dat"
Dim byteTransferRate As Integer = 4096 Dim bytes(byteTransferRate - 1) As Byte
Dim bytesRead As Integer = 0 Dim totalBytesRead As Long = 0 Dim contentLength As Long = 0
contentLength = CLng(response.GetResponseHeader("Content-Length"))
Dim fs As New IO.FileStream(destination, IO.FileMode.Create, _
Dim s As IO.Stream = response.GetResponseStream()
bytesRead = s.Read(bytes, 0, bytes.Length)
If bytesRead > 0 Then
totalBytesRead += bytesRead
fs.Write(bytes, 0, bytesRead)
Loop While bytesRead > 0
s = Nothing
fs = Nothing
response = Nothing
Finally, after we have the file downloaded, perform a little validation just to make sure everything worked as expected.
If totalBytesRead <> contentLength Then
MessageBox.Show("The downloaded file did not download successfully, " & _
"because the length of the downloaded file " & _
"does not match the length of the file on the remote site.", _
"Download File Validation Failed", _
MessageBox.Show("The file has downloaded successfully!", "Download Complete", _
I hope this demo helps you out in your endeavors. If this works for you and you would like to know how to upload a file to a WebDAV server, then please see my article on that.
2008, 2009 Microsoft Visual Basic MVP
- 7th May, 2009: Initial post