Click here to Skip to main content
12,547,806 members (48,030 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked

How to Download a File from a WebDAV Server in VB.NET

, 7 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to download a file from a (HTTPS) WebDAV server in VB.NET


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 HttpWebRequest and 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:

'Get url and Port
Dim url As String = ""
Dim port As String = "443"

'If the port was provided, then insert it into the url.
If port <> "" Then

    'Insert the port into the url
    Dim u As New Uri(url)

    'Get the host (example:
    Dim host As String = u.Host

    'Replace the host with the host:port
    url = url.Replace(host, host & ":" & port)

End If

Next, we can request the file from the WebDAV server.

Dim userName As String = "UserName"
Dim password As String = "Password"

'Create the request
Dim request As HttpWebRequest = _
    DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)

'Set the User Name and Password
request.Credentials = New NetworkCredential(userName, password)

'Let the server know we want to "get" a file
request.Method = WebRequestMethods.Http.Get

'*** This is required for our WebDAV server ***
request.SendChunked = True
request.Headers.Add("Translate: f")

'Get the response from the request
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"

'Create the buffer for storing the bytes read from the server
Dim byteTransferRate As Integer = 4096 '4096 bytes = 4 KB
Dim bytes(byteTransferRate - 1) As Byte
Dim bytesRead As Integer = 0 'Indicates how many bytes were read 
Dim totalBytesRead As Long = 0 'Indicates how many total bytes were read
Dim contentLength As Long = 0 'Indicates the length of the file being downloaded

'Read the content length
contentLength = CLng(response.GetResponseHeader("Content-Length"))

'Create a new file to write the downloaded data to
Dim fs As New IO.FileStream(destination, IO.FileMode.Create, _

'Get the stream from the server
Dim s As IO.Stream = response.GetResponseStream()

    'Read from the stream
    bytesRead = s.Read(bytes, 0, bytes.Length)

    If bytesRead > 0 Then

        totalBytesRead += bytesRead

        'Write to file
        fs.Write(bytes, 0, bytesRead)

    End If

Loop While bytesRead > 0

'Close streams
s = Nothing

fs = Nothing

'Close the response
response = Nothing

Finally, after we have the file downloaded, perform a little validation just to make sure everything worked as expected.

'Validate the downloaded file. Both must be an exact match 
' for the file to be considered a valid download.
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", _
         MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

Else 'totalBytesRead = contentLength

     MessageBox.Show("The file has downloaded successfully!", "Download Complete", _
         MessageBoxButtons.OK, MessageBoxIcon.Information)

End If


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

You may also be interested in...


Comments and Discussions

QuestionGet The File Creation Date Pin
Member 110827728-Dec-14 22:20
memberMember 110827728-Dec-14 22:20 
Question401 error Pin
Member 1020722013-Aug-13 8:20
memberMember 1020722013-Aug-13 8:20 
AnswerRe: 401 error Pin
lighthouse7228-Aug-13 3:13
memberlighthouse7228-Aug-13 3:13 
Question.NET Framework 4 Pin
Dan Martin Christensen23-Jun-13 23:28
memberDan Martin Christensen23-Jun-13 23:28 
GeneralList Files/Directories Pin
kuser15-Oct-09 21:46
memberkuser15-Oct-09 21:46 
GeneralRe: List Files/Directories Pin
VB Rocks29-Oct-09 3:45
memberVB Rocks29-Oct-09 3:45 
GeneralRe: List Files/Directories Pin
kuser29-Oct-09 4:04
memberkuser29-Oct-09 4:04 
GeneralRe: List Files/Directories Pin
VB Rocks29-Oct-09 4:07
memberVB Rocks29-Oct-09 4:07 
GeneralRe: List Files/Directories Pin
kuser29-Oct-09 4:20
memberkuser29-Oct-09 4:20 
GeneralRe: List Files/Directories Pin
maayansv26-Mar-14 3:25
membermaayansv26-Mar-14 3:25 
GeneralRe: List Files/Directories Pin
SgtKashim28-Oct-11 9:03
memberSgtKashim28-Oct-11 9:03 
GeneralWebDAV client in C# Pin
keesvdb10-Aug-09 22:30
memberkeesvdb10-Aug-09 22:30 
GeneralRe: WebDAV client in C# Pin
VB Rocks11-Aug-09 3:43
memberVB Rocks11-Aug-09 3:43 
GeneralProtocol error Pin
Michael Thomsen10-Jun-09 4:27
memberMichael Thomsen10-Jun-09 4:27 
GeneralRe: Protocol error Pin
Michael Thomsen10-Jun-09 4:32
memberMichael Thomsen10-Jun-09 4:32 
GeneralRe: Protocol error Pin
VB Rocks11-Jun-09 4:09
memberVB Rocks11-Jun-09 4:09 
GeneralRe: Protocol error Pin
Michael Thomsen12-Jun-09 3:59
memberMichael Thomsen12-Jun-09 3:59 
GeneralRe: Protocol error Pin
VB Rocks12-Jun-09 10:29
memberVB Rocks12-Jun-09 10:29 
GeneralRe: Protocol error Pin
hpatel3929-Oct-09 9:24
memberhpatel3929-Oct-09 9:24 
GeneralRe: Protocol error Pin
VB Rocks30-Oct-09 7:14
memberVB Rocks30-Oct-09 7:14 
GeneralTwo questions Pin
Michael Thomsen8-Jun-09 8:31
memberMichael Thomsen8-Jun-09 8:31 
GeneralRe: Two questions Pin
VB Rocks9-Jun-09 5:58
memberVB Rocks9-Jun-09 5:58 
GeneralRe: Two questions Pin
Michael Thomsen10-Jun-09 6:30
memberMichael Thomsen10-Jun-09 6:30 
GeneralRe: Two questions Pin
VB Rocks11-Jun-09 3:48
memberVB Rocks11-Jun-09 3:48 
GeneralRe: Two questions Pin
kuser28-Oct-09 4:35
memberkuser28-Oct-09 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 7 May 2009
Article Copyright 2009 by CS Rocks
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid