Pieter
Pieter

Reputation: 2249

How to send OK response to EWS

I am trying to get EWS's push notifications set up in my c# app.

After getting the response from the server and reading it using a NetworkStream I need to respond to the server with Ok in a SOAP message. The only example that I can find uses Microsoft.Web.Services3 and a SoapEnvelope. My understanding is that this has now been replaced by WCF and I really want to use the newer technologies (to learn them).

How would I go by sending a SOAP message back to the server, presumably using the same NetworkStream that I get the notification on?

Here is some code that I tried, but it fails for some reason.

const string RESPONSE_OK = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\"><soap:Body>" +
                "<SendNotificationResult xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">" +
                "<SubscriptionStatus>OK</SubscriptionStatus></SendNotificationResult></soap:Body></soap:Envelope>";

responseBytes = encoding.GetBytes(RESPONSE_OK);

             // Send the result
             HTTPResponseStruct _httpResponse;
            _httpResponse.version = "HTTP/1.1";
            _httpResponse.BodyData = responseBytes;

            _httpResponse.Headers = new Hashtable();
            _httpResponse.Headers.Add("Server", "IT12");
            _httpResponse.Headers.Add("Date", DateTime.Now.ToString("r"));
            _httpResponse.Headers.Add("Content-Type", "text/xml; charset=utf-8");
            _httpResponse.Headers.Add("Content-Length", _httpResponse.BodyData.Length);
            _httpResponse.Headers.Add("Connection", "close");


            string HeadersString = _httpResponse.version + " "
               + "200 OK" + "\r\n";

            foreach (DictionaryEntry Header in _httpResponse.Headers)
            {
                HeadersString += Header.Key + ": " + Header.Value + "\r\n";
            }

            HeadersString += "\r\n";

            byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString);

            // Send headers   
            clientStream.Write(bHeadersString, 0, bHeadersString.Length);


            // Send body
            if (_httpResponse.BodyData != null)
                clientStream.Write(_httpResponse.BodyData, 0,
                         _httpResponse.BodyData.Length);
           // clientStream.Write(responseBytes, 0, responseBytes.Length);
            clientStream.Flush();

Thanks, Pieter

Upvotes: 0

Views: 1264

Answers (2)

Henning Krause
Henning Krause

Reputation: 5422

You marked the other answer as "accepted" but the link you are referring to talks about streaming subscriptions. These are only available for Exchange 2010 and later. For those who are stuck with Exchange Server 2007, there is a Push-Notification library on CodePlex.

Upvotes: 1

Data-Base
Data-Base

Reputation: 8628

you can use Microsoft.Exchange.WebServices.Data (EWS Managed API Reference)

http://msdn.microsoft.com/en-us/library/dd633710%28v=EXCHG.80%29.aspx

Upvotes: 1

Related Questions