tags:

views:

23

answers:

0

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\"&gt;&lt;soap:Body&gt;" +
                "<SendNotificationResult xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\"&gt;" +
                "<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