Today I learned that Exchange 2000 is a lot pickier than Exchange 2003. Specifically, you need to set the content type of a WebDAV request before filling the request stream, or else Exchange 2000 flips out and sends you a 400: Bad Request response. Exchange 2003 doesn't mind that order of operations... so that's where the trouble begins. There's a nice msdn overview here on how to use WebDav to manage contacts, but it uses the latter order... which only works on Exchange 2003. Let's look at some code from their example, slighty edited for brevity:
request.Method = "SEARCH";
// Get a reference to the request stream. Stream requestStream = request.GetRequestStream();
// Write the message body to the request stream.
requestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection for further use. requestStream.Close();
// Set the content type header. request.ContentType = "text/xml";
Works great on Exchange 2003. Blows up inexplicably on Exchange 2000. Here's the (maddeningly simple) fix for Exchange 2000:
request.Method = "SEARCH";
// Set the content type header.
request.ContentType = "text/xml";
// Get a reference to the request stream.
Stream requestStream = request.GetRequestStream();
// Write the message body to the request stream.
requestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection
// for further use.
requestStream.Close();
Hard to even see the difference, right? I'm not entirely sure why this fixes it. Apparently the "content-type: text/xml" header needs to be early in the request for things to work right. And setting the property early puts it there.