Howto: WebDAV PROPFIND using VB.NET


‘ This example shows how to do a PROPFIND using VB.NET


‘ VB.NET PROPFIND
‘ TODO: Create a VB.NET winform with a button and a big multiline text box
‘ TODO: Add references to System.Web, System.XML and System.IO
‘ TODO: Add the code to the form.
‘ TODO: Do the TODO sections in the code.


Imports System.Net
Imports System.Web
Imports System.IO


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ‘Dim sUri As String = “http://myexserver/exchange/User1/Inbox/
    ‘Dim sUserName As String = “Administrator”
    ‘Dim sPassword As String = “test”
    Dim sUserName As String = “”
    Dim sPassword As String = “”
    Dim strBody As String
    strBody = “<?xml version=’1.0′?>” & _
              “<a:propfind xmlns:a=’DAV:’ ” & _
              ”  xmlns:m=’urn:schemas:mailheader:’  ” & _
              ”  xmlns:n=’urn:schemas:httpmail:’>” & _
              “<a:prop>” & _
              ”  <m:subject/>” & _
              ”  <a:isfolder/>” & _
              ”  <a:displayname/>” & _
              ”  <a:href/>” & _
              “</a:prop>” & _
              “</a:propfind>”


    Dim myUri As System.Uri = New System.Uri(sUri)
    Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)


    ‘—————– Set Credentials ————————————–
    Dim myCred As NetworkCredential
    Dim MyCredentialCache As CredentialCache
    If sUserName <> “” Then
        ‘ Use Basic Authentication
        myCred = New NetworkCredential(sUserName, sPassword)
        MyCredentialCache = New CredentialCache
        MyCredentialCache.Add(myUri, “Basic”, myCred)
        HttpWRequest.Credentials = MyCredentialCache
        HttpWRequest.UnsafeAuthenticatedConnectionSharing = True
    Else
        ‘ Use Windows Authentication
        HttpWRequest.Credentials = CredentialCache.DefaultCredentials
        ‘May need this so you dont get an error on HttpWRequest.GetRequestStream()
        ‘or double-hop
        ‘HttpWRequest.UnsafeAuthenticatedConnectionSharing = True
    End If


    ‘ Set Headers
    HttpWRequest.KeepAlive = False
    HttpWRequest.Headers.Set(“Pragma”, “no-cache”)
    HttpWRequest.Headers.Set(“Translate”, “f”)
    HttpWRequest.ContentType = “text/xml”
    HttpWRequest.ContentLength = strBody.Length


    ‘set the request timeout to 5 min.
    HttpWRequest.Timeout = 300000
    ‘ set the request method
    HttpWRequest.Method = “PROPFIND”
   
    ‘ we need to store the data into a byte array
    Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(strBody)
    HttpWRequest.ContentLength = ByteQuery.Length
    Dim QueryStream As Stream = HttpWRequest.GetRequestStream()
    ‘ write the data to be posted to the Request Stream
    QueryStream.Write(ByteQuery, 0, ByteQuery.Length)
    QueryStream.Close()


    ‘ Send Request and Get Response
    Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse()


    ‘ Get Status and Headers
    Dim iStatCode As Integer = HttpWResponse.StatusCode
    Dim sStatus As String = iStatCode.ToString()
    ‘Console.WriteLine(“Status: {0} {1}”, sStatus,    HttpWResponse.StatusDescription.ToString())


    ‘Console.WriteLine(“Request Headers:”)
    ‘Console.WriteLine(HttpWRequest.Headers.ToString())
    ‘Console.WriteLine(“Response Headers:”)
    ‘Console.WriteLine(HttpWResponse.Headers.ToString())


    ‘ Get Response Stream
    Dim strm As Stream = HttpWResponse.GetResponseStream()


    ‘ Read the Response Steam
    Dim sr As StreamReader = New StreamReader(strm)
    Dim sText As String = sr.ReadToEnd()
‘Console.WriteLine(“Response: {0}”, sText)


    ‘ Close Stream
    strm.Close()


    ‘ Clean Up
    HttpWRequest = Nothing
    HttpWResponse = Nothing
    MyCredentialCache = Nothing
    myCred = Nothing
    strm = Nothing
    sr = Nothing


    TextBox1.Text = sText
End Sub

Comments (2)

  1. rehle says:

    Is it possible to get a custom property in this manner?  I have a set of properties that are added  by another process, using the Outlook objects, that i need access too…

    I already have the the href of the item…

Skip to main content