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

namespace CAPushToast
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://db3.notify.live.net/throttledthirdparty/01.00/AAEDHXtMf6OSRo9vQnMJxa5tAgAAAAADAQAAAAQUZm52OjIzOEQ2NDJDRkI5MEVFMEQ";
            string data = string.Format(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                    "<wp:Toast>" +
                        "<wp:Text1>{0}</wp:Text1>" +
                        "<wp:Text2>{1}</wp:Text2>" +
                    "</wp:Toast>" +
                "</wp:Notification>",

                "Hallo", "Ich bin ein Toast");

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

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

            request.Headers.Add("X-WindowsPhone-Target", "toast");
            request.Headers.Add("X-NotificationClass", "2");

            //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());
                }
            }
        }
    }
}
