Getting an Item's Stream (WebDAV)
Topic Last Modified: 2006-06-12
To create non-folder items using the World Wide Web Distributed Authoring and Versioning (WebDAV) protocol
- Create a WebDAV GET Method request. The request Uniform Resource Identifier (URI) for the command is the URI to the item.
- Set the Translate: HTTP header to the value "f" (false).
- Send the request.
- If successful, the response status will be "200 OK."
See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.
This topic contains Microsoft® JScript®, Microsoft Visual C++®, Microsoft C#, and Microsoft Visual Basic® .NET code examples.
The following example shows a typical GET Method request:
GET /pub2/folder1/folder2/item1.eml HTTP/1.1
Host: hostname
Translate: f
JScript
The following example uses an instance of the Microsoft.XMLHTTP Component Object Model (COM) class to send a GET Method request to an Exchange store server.
Example
/*
** The getStream function returns a reference to an
** XMLHTTP object that should contain the stream for the
** specified item.
*/
// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
function getStream( uri ){
// Initialize the XMLHTTP request object.
var Req = new ActiveXObject("Microsoft.XMLHTTP");
// Open the request object with the GET method and
// specify that it will be sent asynchronously.
Req.open("GET", uri, false);
// Set the Translate header to False.
Req.setRequestHeader("Translate","f");
// Send the GET method request.
Req.send();
// An error occurred on the server.
if(Req.status >= 500)
{
this.document.writeln("Status: " + Req.status);
this.document.writeln("Status text: An error occurred on the server.");
}
else
{
this.document.writeln("Status: " + Req.status);
this.document.writeln("Status text: " + Req.statustext);
}
return Req;
}
C++
The following example uses the GET method to get the test.eml resource's stream.
Example
#include <stdio.h>
#include <iostream.h>
// If necessary, change the file path if your msxml.dll file is
// in a different location.
#import "c:\windows\system32\msxml.dll"
// To use MSXML 4.0, import the dll msxml4.dll instead of msxml.dll as follows:
// #import "c:\windows\system32\msxml4.dll"
// using namespace MSXML2;
using namespace MSXML;
int main(int argc, char* argv[])
{
CoInitialize(NULL);
// Variables.
MSXML::IXMLHttpRequest* pXMLHttpReq=NULL;
bstr_t sUrl = "https://server/public/TestFolder/test.eml";
bstr_t sMethod = "GET";
_variant_t vUser = L"Domain\\Username";
_variant_t vPassword = L"!Password";
_variant_t vAsync = (bool)FALSE;
long lStatus = 0;
BSTR bstrResp;
BSTR bstrResponseText;
HRESULT hr;
// Initialize the XMLHTTPRequest object pointer.
hr = ::CoCreateInstance(__uuidof(XMLHTTPRequest),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLHttpRequest),
(LPVOID*)&pXMLHttpReq);
// If you are using MSXML 4.0, use the following to initialize pXMLHttpReq:
// IXMLHTTPRequestPtr pXMLHttpReq= NULL;
// HRESULT hr=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");
// Check the status of pointer creation.
if (S_OK != hr)
{
cout << "XMLHttpRequest pointer creation failed." << endl;
return 1;
}
try
{
// Open the XMLHTTPRequest object with the GET method and
// specify that it will be sent asynchronously.
pXMLHttpReq->open(sMethod,
sUrl,
vAsync,
vUser,
vPassword);
// Set the Translate header.
pXMLHttpReq->setRequestHeader((bstr_t)"Translate", (bstr_t)"F");
// Send the GET method request.
pXMLHttpReq->send();
// Get the response status.
pXMLHttpReq->get_status(&lStatus);
// An error occurred on the server.
if(lStatus >= 500)
{
cout << "Status: " << lStatus << endl
<< "Status text: An error occurred on the server."
<< endl;
}
else
{
// Display the response status.
cout << "Status: " << lStatus << endl;
// Display the response status text.
pXMLHttpReq->get_statusText(&bstrResp);
cout << "Status text: " << (char*)(bstr_t)bstrResp << endl;
// Display the response text.
pXMLHttpReq->get_responseText(&bstrResponseText);
cout << "Response text: " << (char*)(bstr_t)bstrResponseText << endl;
}
// Release the memory.
pXMLHttpReq->Release();
}
catch(_com_error &e)
{
// Display the error information.
cout << "Error code: " << (char*)e.Error() << endl
<< "Error message: " << (char*)e.ErrorMessage()
<<endl;
// Release the memory.
pXMLHttpReq->Release();
return 1;
}
CoUninitialize();
return 0;
}
C#
The following example uses the GET method to get the test.eml resource's stream.
Example
using System;
using System.Net;
using System.Text;
using System.IO;
namespace ExchangeSDK.Snippets.CSharp
{
class GettingAnItemsStreamWebDAV
{
[STAThread]
static void Main(string[] args)
{
// Variables.
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strSrcURI = "https://server/TestStore/TestStoreFolder/test.eml";
string strUserName = "UserName";
string strPassword = "!Password";
string strDomain = "Domain";
System.IO.Stream ResponseStream;
System.IO.StreamReader sr;
try
{
// Create a new CredentialCache object and fill it with the network
// credentials required to access the server.
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add( new System.Uri(strSrcURI),
"NTLM",
new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
);
// Create the HttpWebRequest object.
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);
// Add the network credentials to the request.
Request.Credentials = MyCredentialCache;
// Specify the method.
Request.Method = "GET";
// Set the Translate header.
Request.Headers.Add("Translate","f");
// Send the GET method request and get the
// response from the server.
Response = (HttpWebResponse)Request.GetResponse();
// Display the item's stream content type and length.
Console.WriteLine("Content type: " + Response.ContentType);
Console.WriteLine("Content length: " + Response.ContentLength);
// Create the stream reader object with the
// response stream.
ResponseStream = Response.GetResponseStream();
sr = new StreamReader(ResponseStream, Encoding.UTF8);
// Display the item's stream.
Console.WriteLine("Item stream:\n");
Console.WriteLine(sr.ReadToEnd());
// Clean up.
Response.Close();
ResponseStream.Close();
sr.Close();
}
catch(Exception ex)
{
// Catch any exceptions. Any error codes from the GET
// method request on the server will be caught here, also.
Console.WriteLine(ex.Message);
}
}
}
}
Visual Basic .NET
The following example uses the GET method to get the test.eml resource's stream.
Example
Option Explicit On
Option Strict On
Module Module1
Sub Main()
' Variables.
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.HttpWebResponse
Dim MyCredentialCache As System.Net.CredentialCache
Dim strPassword As String
Dim strDomain As String
Dim strUserName As String
Dim strSrcURI As String
Dim bytes() As Byte
Dim ResponseStream As System.IO.Stream
Dim sr As System.IO.StreamReader
Try
' Initialize variables.
strUserName = "UserName"
strPassword = "!Password"
strDomain = "Domain"
strSrcURI = "https://server/TestStore/TestStoreFolder/test.eml"
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strSrcURI), _
"NTLM", _
New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
)
' Create the HttpWebRequest object.
Request = CType(System.Net.WebRequest.Create(strSrcURI), _
System.Net.HttpWebRequest)
' Add the network credentials to the request.
Request.Credentials = MyCredentialCache
' Specify the GET method.
Request.Method = "GET"
' Set the Translate header.
Request.Headers.Add("Translate", "f")
' Send the GET method request and get the
' response from the server.
Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)
' Display the item's stream content type and length.
Console.WriteLine("Content type: " & Response.ContentType)
Console.WriteLine("Content length: " & Response.ContentLength)
' Create the stream reader object with the
' response stream.
ResponseStream = Response.GetResponseStream()
sr = New System.IO.StreamReader(ResponseStream, _
System.Text.Encoding.UTF8)
' Display the item's stream.
Console.WriteLine("Item stream:")
Console.WriteLine("")
Console.WriteLine(sr.ReadToEnd())
' Clean up.
Response.Close()
ResponseStream.Close()
sr.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
' GET method requests on the server will be caught
' here, also.
Console.WriteLine(ex.Message)
End Try
End Sub
End Module