Microsoft KB Archive/313123

= How to retrieve an object's properties by using the HttpWebRequest class and the HttpWebResponse class in Visual C# =

Article ID: 313123

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 Q313123



SUMMARY
This article describes how to use the HttpWebRequest class with the PROPFIND method and the HttpWebResponse class from the System.Net namespace to retrieve the properties of an object on Microsoft Exchange 2000 Server in Microsoft Visual C#.



MORE INFORMATION
To retrieve the properties of an object, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, select Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types. Under Templates, select Console Application, and then click OK.

By default, Class1.cs is created in Visual Studio .NET, and the Program.cs file is created in Visual Studio 2005..  In the code window, replace all 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 on Exchange 2000. string sUri = &quot;http://ExchServer/Exchange/UserAlias/Inbox/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;<m:subject/>&quot; + &quot;<a:isfolder/>&quot; + &quot;<a:displayname/>&quot; + &quot;<a:href/>&quot; + &quot;</a:prop>&quot; + &quot;</a:propfind>&quot;;

// Set credentials. // TODO: Replace with appropriate user credentials. NetworkCredential myCred = new NetworkCredential(@&quot;DomainName\UserName&quot;, &quot;UserPassword&quot;); CredentialCache myCredentialCache = new CredentialCache; myCredentialCache.Add(myUri, &quot;Basic&quot;, myCred); HttpWRequest.Credentials = myCredentialCache; //Uncomment the following statement and comment the previous four statements if you //use Integrated Windows authentication. //httpWRequest.Credentials = CredentialCache.DefaultCredentials

//Note In Basic type authentication, the user name and password are sent as base64-encoded text, and it is                //easily decoded. Microsoft recommends that you use Basic instead of SSL to help protect the user name and password.

// Set headers. HttpWRequest.KeepAlive = true; //this is the default 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 five minutes. HttpWRequest.Timeout = 300000; // set the request method HttpWRequest.Method = &quot;PROPFIND&quot;;

// 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 the request and get a 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 the 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> Press F5 to build and to run the program.</li> Make sure that the object's properties have been retrieved from the response stream.</li></ol>

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

Keywords: kbxml kbcode kbmsg kbhowto KB313123

-

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

© Microsoft Corporation. All rights reserved.