using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace CAPushRAW
{
    class Program
    {
        static void Main(string[] args)
        {
            string url =
                "http://db3.notify.live.net/throttledthirdparty/01.00/AAGOIXco81BoSY_1tAv8HeM0AgAAAAADAQAAAAQUZm52OjIzOEQ2NDJDRkI5MEVFMEQ";
            string data =
@"<?xml version='1.0'?>
<catalog>
    <book id='bk101'>
        <author>Gambardella, Matthew</author>
    </book>
</catalog>";


            //Erstellen eines neuen Webrequest mit der vom Gerät empfangenen Adresse
            WebRequest request = WebRequest.Create(url);

            request.Headers.Add("X-NotificationClass", "3");

            //Es werden Benachrichtigungen immer nur im XML Format versand
            request.ContentType = "text/xml";
            request.Method = "POST";


            //Schreiben der Nachricht in den Request-Stream
            byte[] bytes = Encoding.UTF8.GetBytes(data);

            request.ContentLength = bytes.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);

                try
                {
                    //Auslesen des Antwortstatus
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    string notificationStatus = response.Headers["X-NotificationStatus"];
                    string subscriptionStatus = response.Headers["X-SubscriptionStatus"];
                    string connectionStatus = response.Headers["X-DeviceConnectionStatus"];

                    Console.Write(
                        String.Format("X-NotificationStatus:{0}\r\nX-SubscriptionStatus:{1}\r\nX-DeviceConnectionStatus:{2}",
                        notificationStatus, subscriptionStatus, connectionStatus));
                    Console.ReadLine();
                }
                catch (WebException ex)
                {
                    Console.Write("WebException occured with Statuscode:" +
                        ((HttpWebResponse)ex.Response).StatusCode.ToString());
                }
            }
        }
    }
}
