Microsoft KB Archive/313118

= How to copy and move objects on a computer that is running Exchange 2000 Server by using the HttpWebRequest class and the HttpWebResponse class in Visual C# =

Article ID: 313118

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q313118



SUMMARY
This article describes how to use the HttpWebRequest class and the HttpWebResponse class from the System.Net namespace to copy and move items on a computer that is running Microsoft Exchange 2000 Server by using Microsoft Visual C#.

This article assumes that you are familiar with the following topics:
 * Programming with Visual C#
 * The HttpWebRequest class and the HttpWebResponse class
 * The Web Distributed Authoring and Versioning (WebDAV) protocol

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET or Microsoft Visual 2005
 * Exchange 2000



MORE INFORMATION
To copy and move items on a computer that is running Exchange 2000 by using Visual C#, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects typeslist, click Console Application.

Note In Visual Studio 2005, click Console Application in the Visual C# list.

In Visual Studio .NET, the Class1.cs file is created by default. In Visual Studio 2005, Program.cs is created by default.  In the code window, replace the code with the following: using System; using System.Net; using System.IO;

namespace WebDavNET { ///   /// Summary description for Class1. ///  class Class1 {   static void Main(string[] args) {     try {       bool bMove; // TODO: Uncomment the following line to Move // bMove = true; // TODO: Uncomment the following line to Copy // bMove = false;

// TODO: Replace with the source URL of an item String sSourceURL = &quot;http://ExchServer/Public/Folder1/test1.eml&quot;; // TODO: Replace with the destination URL of an item String sDestinationURL = &quot;http://ExchServer/Public/Folder2/test2.eml&quot;; System.Uri myUri; myUri = new System.Uri(sSourceURL); HttpWebRequest HttpWRequest; HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

// Set credentials. NetworkCredential myCred; // TODO: Replace with the appropriate username and password myCred = new NetworkCredential(@&quot;DomainName\UserName&quot;, &quot;UserPassword&quot;); CredentialCache myCredentialCache; myCredentialCache = new CredentialCache; myCredentialCache.Add(myUri, &quot;Basic&quot;, myCred); HttpWRequest.Credentials = myCredentialCache;

// Set headers. HttpWRequest.KeepAlive = false; HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;); HttpWRequest.Headers.Set(&quot;Destination&quot;, sDestinationURL);

//Set the request timeout to 5 minutes. HttpWRequest.Timeout = 300000; // set the request method if(bMove) {         HttpWRequest.Method = &quot;MOVE&quot;; }       else {         HttpWRequest.Method = &quot;COPY&quot;; }

// Send the request and get the response. HttpWebResponse HttpWResponse; HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse;

// Get the Status code. int iStatCode = (int)HttpWResponse.StatusCode; string sStatus = iStatCode.ToString; Console.WriteLine(&quot;Status Code: {0}&quot;, sStatus); // Get the request headers string sReqHeaders = HttpWRequest.Headers.ToString; Console.WriteLine(sReqHeaders);

// Read the response stream. Stream strm = HttpWResponse.GetResponseStream; StreamReader sr = new StreamReader(strm); string sText = sr.ReadToEnd; Console.WriteLine(&quot;Response: {0}&quot;, sText);

// Close the stream. strm.Close;

// Clean up       myCred = null; myCredentialCache = null; HttpWRequest = null; HttpWResponse = null; strm = null; sr = null; }     catch (Exception e)      { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }   }  }

}  Search for TODO in the code, and then modify the code for your environment. Press F5 to build and to run the program. Make sure that the item was copied and moved.</li></ol>

<div class="references_section">