Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
4.90/5 (14 votes)
7 May 2009CPOL2 min read 113.8K   2.7K   31   36
This article demonstrates how to download a file from a (HTTPS) WebDAV server in VB.NET
Image 1

Introduction

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.

Background

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:

VB.NET
'Get url and Port
Dim url As String = "https://someSecureTransferSite.com/fileToDownload.dat"
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
    'https://www.example.com:443/fileToDownload.dat
    Dim u As New Uri(url)

    'Get the host (example: www.example.com)
    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.

VB.NET
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.

VB.NET
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, _
       IO.FileAccess.Write)

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

Do
    '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.Close()
s.Dispose()
s = Nothing

fs.Close()
fs.Dispose()
fs = Nothing

'Close the response
response.Close()
response = Nothing

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

VB.NET
'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

Conclusion

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.

VBRocks
2008, 2009 Microsoft Visual Basic MVP

History

  • 7th May, 2009: Initial post

License

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


Written By
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

 
QuestionGet The File Creation Date Pin
Member 110827728-Dec-14 22:20
Member 110827728-Dec-14 22:20 
Question401 error Pin
Member 1020722013-Aug-13 8:20
Member 1020722013-Aug-13 8:20 
AnswerRe: 401 error Pin
lighthouse7228-Aug-13 3:13
lighthouse7228-Aug-13 3:13 
Question.NET Framework 4 Pin
Dan Martin Christensen23-Jun-13 23:28
Dan Martin Christensen23-Jun-13 23:28 
GeneralList Files/Directories Pin
kuser15-Oct-09 21:46
kuser15-Oct-09 21:46 
GeneralRe: List Files/Directories Pin
CS Rocks29-Oct-09 3:45
CS Rocks29-Oct-09 3:45 
GeneralRe: List Files/Directories Pin
kuser29-Oct-09 4:04
kuser29-Oct-09 4:04 
GeneralRe: List Files/Directories Pin
CS Rocks29-Oct-09 4:07
CS Rocks29-Oct-09 4:07 
GeneralRe: List Files/Directories Pin
kuser29-Oct-09 4:20
kuser29-Oct-09 4:20 
GeneralRe: List Files/Directories Pin
maayansv26-Mar-14 3:25
maayansv26-Mar-14 3:25 
GeneralRe: List Files/Directories Pin
SgtKashim28-Oct-11 9:03
SgtKashim28-Oct-11 9:03 
GeneralWebDAV client in C# Pin
keesvdb10-Aug-09 22:30
keesvdb10-Aug-09 22:30 
GeneralRe: WebDAV client in C# Pin
CS Rocks11-Aug-09 3:43
CS Rocks11-Aug-09 3:43 
Hi Kees, Thank you for taking the time to read my article and post a comment!

Also, thank you for the C# client as well.

Concerning what WebDAV server I used SendChunks and the translate header tricks on: I'm not completely sure. My company had an internal FTP site that used to transmit data to and from our customers. Because of new SAS 70 requirements, we outsourced it to an external IT company that set up our WebDAV server... So I don't know how they set it up, or configured it. I do know that regular FTP software would not work with it. The IT company had us setup Windows WebDAV folders to access the site. When I wrote my program, I did extensive research about interacting with WebDAV folders, and once I got it nailed down it worked like a charm!

Thanks again!
GeneralProtocol error Pin
Michael Thomsen10-Jun-09 4:27
Michael Thomsen10-Jun-09 4:27 
GeneralRe: Protocol error Pin
Michael Thomsen10-Jun-09 4:32
Michael Thomsen10-Jun-09 4:32 
GeneralRe: Protocol error Pin
CS Rocks11-Jun-09 4:09
CS Rocks11-Jun-09 4:09 
GeneralRe: Protocol error Pin
Michael Thomsen12-Jun-09 3:59
Michael Thomsen12-Jun-09 3:59 
GeneralRe: Protocol error Pin
CS Rocks12-Jun-09 10:29
CS Rocks12-Jun-09 10:29 
GeneralRe: Protocol error Pin
himanshubpatel29-Oct-09 9:24
himanshubpatel29-Oct-09 9:24 
GeneralRe: Protocol error Pin
CS Rocks30-Oct-09 7:14
CS Rocks30-Oct-09 7:14 
GeneralTwo questions Pin
Michael Thomsen8-Jun-09 8:31
Michael Thomsen8-Jun-09 8:31 
GeneralRe: Two questions Pin
CS Rocks9-Jun-09 5:58
CS Rocks9-Jun-09 5:58 
GeneralRe: Two questions Pin
Michael Thomsen10-Jun-09 6:30
Michael Thomsen10-Jun-09 6:30 
GeneralRe: Two questions Pin
CS Rocks11-Jun-09 3:48
CS Rocks11-Jun-09 3:48 
GeneralRe: Two questions Pin
kuser28-Oct-09 4:35
kuser28-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.