How to use the HttpWebRequest class and the HttpWebResponse class to delete an object by using Visual Basic .NET (314193)



The information in this article applies to:

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

This article was previously published under Q314193

SUMMARY

This article describes how to use the HttpWebRequest class and the HttpWebResponse class of the "System.Net" namespace to delete an object on a computer that is running Microsoft Exchange 2000 Server by using Microsoft Visual Basic .NET.

MORE INFORMATION

To delete an object on a computer that is running Exchange 2000 by using Visual Basic .NET, follow these steps.

Note The following sample code deletes the NewFolder folder in the Inbox. If the NewFolder folder does not exist in the Inbox, you receive the following error message:
Unhandled Exception: System.Net.WebException: The remote server returned an error: (404) Not Found.
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects types list, click Console Application.

    By default, the Module1.vb file is created.
  4. In the code window, replace the code with the following:
    Imports System.Net
    Imports System.IO
    
    Module Module1
        Sub Main()
            ' TODO: Replace with the object URL that is to be deleted.
            Dim sUri As String = "http://<ExchServer>/Exchange/<UserAlias>/Inbox/NewFolder"
    
            Dim myUri As System.Uri = New System.Uri(sUri)
            Dim HttpWRequest As HttpWebRequest = WebRequest.Create(myUri)
    
            ' Set Credentials
            ' TODO: Replace with an appropriate user credential.
            Dim myCred As NetworkCredential = New NetworkCredential("Domain\UserName", "Password")
            Dim MyCredentialCache As CredentialCache = New CredentialCache()
            MyCredentialCache.Add(myUri, "Basic", myCred)
            HttpWRequest.Credentials = MyCredentialCache
    
            'Uncomment the following statement and comment the previous 4 statements if you
            'use Integrated Windows authentication
            'HttpWRequest.Credentials = CredentialCache.DefaultCredentials
    
            'Note In Basic type authentication, username and password are sent as base64-encoded text and is 
            'easily decoded. Microsoft recommends that you use Basic instead of SSL to help protect the username and password.        
    
            ' Set Headers
            HttpWRequest.KeepAlive = False
            HttpWRequest.Headers.Set("Pragma", "no-cache")
    
            'Set the request timeout to 5 minutes.
            HttpWRequest.Timeout = 300000
            ' set the request method
            HttpWRequest.Method = "DELETE"
    
            ' 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. Search for TODO in the code, and then modify the code for your environment.
  6. Press F5 to build and to run the program.
  7. Make sure that the object has been deleted.

Modification Type:MajorLast Reviewed:4/9/2004
Keywords:kbhowto KB314193 kbAudDeveloper