Microsoft KB Archive/313126

= How to use the HttpWebRequest object and the HttpWebResponse object to modify the urn:schemas:mailheader:subject property in Visual C# =

Article ID: 313126

Article Last Modified on 12/11/2006

-

APPLIES TO


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

-



This article was previously published under Q313126



SUMMARY
This article describes how to use the HttpWebRequestobject and the HttpWebResponseobject from the &quot;System.Net&quot; namespace to modify the urn:schemas:mailheader:subject property for an item in Microsoft Exchange 2000 Server in Microsoft Visual C#.



MORE INFORMATION
To modify the urn:schemas:mailheader:subject property for an item in Visual C#, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft 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.

n Visual Studio .NET, Class1.cs 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 {           // TODO: Replace with the URL of an object in Exchange Server string sUri = &quot;http://ExchServer/Public/MyFolder/Test.EML&quot;;

System.Uri myUri = new System.Uri(sUri); HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

string sQuery; sQuery = &quot;&quot; + &quot;&quot; + &quot;&quot; + &quot;&quot; + &quot;ModifiedSubject&quot; + &quot;</m:subject>&quot; + &quot;</a:prop></a:set>&quot; + &quot;</a:propertyupdate>&quot;;

// Set Credentials // TODO: Replace with appropriate user credential NetworkCredential myCred = new NetworkCredential(@&quot;DomainName\UserName&quot;, &quot;Password&quot;); CredentialCache myCredentialCache = new CredentialCache; myCredentialCache.Add(myUri, &quot;Basic&quot;, 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, the username and the password are sent as base64-encoded text, which is            //easily decoded. Microsoft recommends that you use Basic over SSL to help protect the username and the password.

// Set Headers HttpWRequest.KeepAlive = false; HttpWRequest.Headers.Set(&quot;Pragma&quot;, &quot;no-cache&quot;); HttpWRequest.Headers.Set(&quot;Translate&quot;, &quot;f&quot;); HttpWRequest.ContentType = &quot;text/xml&quot;; HttpWRequest.ContentLength = sQuery.Length;

//set the request timeout to 5 min.           HttpWRequest.Timeout = 300000; // set the request method HttpWRequest.Method = &quot;PROPPATCH&quot;;

// You must store the data in a byte array byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery); HttpWRequest.ContentLength = ByteQuery.Length; Stream QueryStream = 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 HttpWebResponse 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 Stream strm.Close;

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

} </li> Search for TODO in the code, and then modify the code for your environment.</li> Build and then run the program.</li> Make sure that the subject of the specified item has been modified.</li></ol>

Additional query words: HttpWebRequest,HttpWebResponse,System.Net,modify, item,Exchange 2000 Server

Keywords: kbmsg kbxml kbcode kbhowto KB313126

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.