Click here to Skip to main content
13,201,763 members (70,993 online)
Click here to Skip to main content
Add your own
alternative version

Stats

74.2K views
2.1K downloads
31 bookmarked
Posted 7 May 2009

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

, 7 May 2009
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

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:

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

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, _
       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.

'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)

Share

About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

You may also be interested in...

Pro
Pro

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 

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.

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