Howto: Post XML to EWS using ExchangeServiceBinding credentials.

Here is a sample which shows how to use the connection of the Exchange Service Binding to do a POST to EWS using an XML string.

// Sample calling code:

private void cmdExecute_Click(object sender, EventArgs e)

{

    string sRequest = string.Empty;

    bool bRet = false;

    

    string sResponse = string.Empty;

    bRet = EwsHelper.DoSoapRequest(

                sURL,

                oExchangeServiceBinding.Credentials,

                sRequestXml,

                ref sResponse

                );

    txtResponse.Text = sResponse;

}

// Sample of implementation:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Xml;

using System.Xml.Serialization;

using System.Net;

using System.Windows.Forms;

using MyExplorerApp.MyWebServiceRef;

// -------------------------------------------------------------------------------------------------------------

// DoSoapRequest

// sEwsUrl - this is the url to the exchange.asmx

// oCredentials - this is the credentials object from the ExchangeServiceBinding.

// EWSRequestString - What you are requesting

// EWSResponseString - The response string.

// -------------------------------------------------------------------------------------------------------------

public static bool DoSoapRequest(string sEwsUrl, ICredentials oCredentials, string EWSRequestString, ref string EWSResponseString)

{

    bool bError = false;

    EWSResponseString = "";

    HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(sEwsUrl);

    webRequest.Method = "POST";

    webRequest.ContentType = "text/xml;utf-8";

    

    webRequest.Credentials = oCredentials;

    byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(EWSRequestString);

    webRequest.ContentLength = requestBytes.Length;

    using (Stream requestStream = webRequest.GetRequestStream()) // Fill Request.

    {

        requestStream.Write(requestBytes, 0, requestBytes.Length);

        requestStream.Flush();

        requestStream.Close();

    }

    string sError = string.Empty;

    HttpWebResponse webResponse = null;

    try

    {

        webResponse = webRequest.GetResponse() as HttpWebResponse; // Get Response

    }

    catch (WebException webException)

    {

        HttpWebResponse httpResponse = webException.Response as HttpWebResponse;

        using (Stream responseStream = httpResponse.GetResponseStream())

        {

            StreamReader reader = new StreamReader(responseStream);

            sError = reader.ReadToEnd();

      MessageBox.Show(sError, "Error");

            bError = true;

        }

    }

    XmlDocument doc = new XmlDocument();

    // Read Response Stream

    if (bError == false)

    {

        string sResponse = string.Empty;

        using (Stream responseStream = webResponse.GetResponseStream())

        {

            StreamReader reader = new StreamReader(responseStream, Encoding.ASCII);

            sResponse = reader.ReadToEnd();

        }

        EWSResponseString = sResponse;

    }

    webRequest.Credentials = null;

    return bError;

}