
February 26, 2009 19:25 by
Simon
It is possible to interface with exchange - create public folders etc, using WebDAV. The next few posts will include code and an explanation of how to go about doing a number of things such as creating public folders. In order to be able to do anything you need to be able to send WebDAV xml documents via an Http request, below are a number of methods which can be used to do this, subsequent posts will make use of these methods. The first parameter passed to each of these methods is a cookie collection. If Outlook Web Access (OWA) is enabled this collection should contain the cookies returned after a successful login - Part 2 of this series of posts will show how to login if OWA is enabled. If OWA is not enabled passing null or an empty collection will mean that the code will use the default credentials to login i.e. the login details of whoever the code is running as.
/// <summary>
/// Sends a PROPFIND Http Request.
/// </summary>
/// <param name="cookies">The cookies returned from previously authenticating.</param>
/// <param name="URI">The URI to send the request to.</param>
/// <param name="depth">The value of the depth header.</param>
/// <param name="content">XML content.</param>
/// <returns>A Http Response.</returns>
private static HttpWebResponse SendPropFindRequest(CookieCollection cookies,
string URI,
string depth,
string content)
{
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Depth", depth);
return SendRequest(cookies, URI, "PROPFIND", headers, content);
}
/// <summary>
/// Sends a Http Request with text/xml content.
/// </summary>
/// <param name="cookies">The cookies returned from previously authenticating.</param>
/// <param name="URI">The URI to send the request to.</param>
/// <param name="method">The request method to use.</param>
/// <param name="headers">Optional Http request headers.</param>
/// <param name="content">XML content.</param>
/// <returns>A Http Response.</returns>
private static HttpWebResponse SendRequest(CookieCollection cookies,
string URI,
string method,
IDictionary<string, string> headers,
string content)
{
byte[] bytes = null;
if (content != null)
{
bytes = Encoding.UTF8.GetBytes(content);
}
return SendRequest(cookies, URI, method, "text/xml", headers, bytes);
}
/// <summary>
/// Sends a Http Request to the URI.
/// </summary>
/// <param name="cookies">Cookies to be sent with the request (i.e. obtained from a previous logon).</param>
/// <param name="URI">The URI to send the request to.</param>
/// <param name="method">The request method.</param>
/// <param name="contentType">The content type e.g. text/xml.</param>
/// <param name="headers">Any additional headers to send.</param>
/// <param name="content">The content to send.</param>
/// <returns>A Http Response.</returns>
private static HttpWebResponse SendRequest(CookieCollection cookies,
string URI,
string method,
string contentType,
IDictionary<string, string> headers,
byte[] content)
{
ServicePointManager.ServerCertificateValidationCallback += AllowAllSites;
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);
// Add cookies required for Outlook Web Access (OWA) Forms Authentication if there aren't any
// then use the default credentials instead (i.e. the user that the service is running as)
request.CookieContainer = new CookieContainer();
if (cookies != null && cookies.Count > 0)
{
request.CookieContainer.Add(cookies);
}
else
{
request.UseDefaultCredentials = true;
}
request.Method = method;
request.ContentType = contentType;
// Add optional headers.
if (headers != null)
{
foreach (string key in headers.Keys)
{
request.Headers.Set(key, headers[key]);
}
}
// Add optional content
if (content != null)
{
request.ContentLength = content.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(content, 0, content.Length);
}
}
return (HttpWebResponse)request.GetResponse();
}
finally
{
ServicePointManager.ServerCertificateValidationCallback -= AllowAllSites;
}
}
/// <summary>
/// Used to accept all certificates.
/// </summary>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="chain"></param>
/// <param name="errors"></param>
/// <returns>true</returns>
private static Boolean AllowAllSites(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
3e711e7f-5dd6-4734-8446-2e921f48c3c2|0|.0