How to use the HttpWebRequest class and the HttpWebResponse class to modify the properties of an object in Visual Basic .NET (314198)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Exchange 2000 Server

This article was previously published under Q314198
For a Microsoft Visual C# .NET version of this article, see 313126.

INTRODUCTION

This article describes how to use the HttpWebRequest class and the HttpWebResponse class from the System.Net namespace to modify the urn:schemas:mailheader:subject property. You can do this for an item in Microsoft Exchange 2000 Server by using Microsoft Visual Basic .NET.

MORE INFORMATION

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Visual Basic Projects types, click Console Application

    By default, Module1.vb is created.
  4. Replace all the code in the code window with the following code:
    Imports System.Net
    Imports System.IO
    
    Module Module1
        Sub Main()
            ' TODO: Replace with the URL of an object on the Exchange Server.
            Dim sUri As String = "http://ExchServer/Public/MyFolder/Test.EML"
    
            Dim myUri As System.Uri = New System.Uri(sUri)
            Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)
    
            ' TODO: Change the Subject to "ModifiedSubject".  
            Dim sQuery As String
            sQuery = "<?xml version='1.0'?>" & _
             "<a:propertyupdate xmlns:a='DAV:' " & _
             "xmlns:m='urn:schemas:mailheader:'>" & _
             "<a:set><a:prop>" & _
             "<m:subject>" & "ModifiedSubject" & "</m:subject>" & _
             "</a:prop></a:set>" & _
             "</a:propertyupdate>"
    
            ' TODO: Replace with the appropriate user credentials.
            Dim myCred As NetworkCredential = New NetworkCredential("Domain\UserName", "Password")
            Dim MyCredentialCache As CredentialCache = New CredentialCache()
            MyCredentialCache.Add(myUri, "Basic", myCred)
            HttpWRequest.Credentials = MyCredentialCache
    
            ' Set the Headers.
            HttpWRequest.KeepAlive = False
            HttpWRequest.Headers.Set("Pragma", "no-cache")
            HttpWRequest.Headers.Set("Translate", "f")
            HttpWRequest.ContentType = "text/xml"
            HttpWRequest.ContentLength = sQuery.Length
            'Set the request timeout to 5 minutes.
            HttpWRequest.Timeout = 300000
            ' Set the request method.
            HttpWRequest.Method = "PROPPATCH"
    
            ' Store the data in a byte array.
            Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(sQuery)
            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 the Request and Get the Response.
            Dim HttpWResponse As HttpWebResponse = HttpWRequest.GetResponse()
    
            ' Get the Status and the 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 the Response Stream.
            Dim strm As Stream = HttpWResponse.GetResponseStream()
    
            ' Read the Response Stream.
            Dim sr As StreamReader = New StreamReader(strm)
            Dim sText As String = sr.ReadToEnd()
            Console.WriteLine("Response: {0}", sText)
    
            ' Close the Stream.
            strm.Close()
    
            ' Clean up.
            HttpWRequest = Nothing
            HttpWResponse = Nothing
            MyCredentialCache = Nothing
            myCred = Nothing
            strm = Nothing
            sr = Nothing
        End Sub
    End Module
  5. Modify the previous code on the lines where you see TODO.
  6. Press F5 to build and to run the program.
  7. Verify that the subject of the specified item has been changed.

Modification Type:MajorLast Reviewed:4/16/2004
Keywords:kbhowto KB314198 kbAudDeveloper