public async Task <WnsPushResult> PushAsync(WnsAuthentication authentication, string uri, XmlDocument doc, NotificationType type)
        {
            // create...
            var content = new StringContent(doc.OuterXml);

            content.Headers.ContentType.MediaType = "text/xml";

            // if...
            if (type == NotificationType.Toast)
            {
                content.Headers.Add("X-WNS-Type", "wns/toast");
            }
            else if (type == NotificationType.Tile)
            {
                content.Headers.Add("X-WNS-Type", "wns/tile");
            }
            else if (type == NotificationType.Badge)
            {
                content.Headers.Add("X-WNS-Type", "wns/badge");
            }
            else if (type == NotificationType.Raw)
            {
                content.Headers.Add("X-WNS-Type", "wns/badge");
            }
            else
            {
                throw new NotSupportedException(string.Format("Cannot handle '{0}'.", type));
            }

            // ok...
            var client   = authentication.GetHttpClient();
            var response = await client.PostAsync(uri, content);

            // what happened?
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // what happened?
                var all = response.Headers.Where(v => v.Key == "X-WNS-NOTIFICATIONSTATUS").FirstOrDefault();
                if (string.IsNullOrEmpty(all.Key))
                {
                    throw new InvalidOperationException("'X-WNS-NOTIFICATIONSTATUS' header not returned.");
                }
                return((WnsPushResult)Enum.Parse(typeof(WnsPushResult), all.Value.First(), true));
            }
            else
            {
                throw await WnsAuthenticator.CreateRequestException("Failed to post notification.", response);
            }
        }
Пример #2
0
        private async void buttonAuthenticate_Click(object sender, EventArgs e)
        {
            this.buttonAuthenticate.Enabled = false;
            try
            {
                SaveSettings();

                this.Authentication = null;

                string sid = this.textSid.Text.Trim();
                if (string.IsNullOrEmpty(sid))
                {
                    MessageBox.Show(this, "You must supply a SID.");
                    return;
                }
                string secret = this.textSecret.Text.Trim();
                if (string.IsNullOrEmpty(secret))
                {
                    MessageBox.Show(this, "You must supply a secret");
                    return;
                }

                // get the token...
                var authenticator = new WnsAuthenticator();
                this.Authentication = await authenticator.AuthenticateAsync(sid, secret);

                // et...
                this.textToken.Text = this.Authentication.Token;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString());
            }
            finally
            {
                this.buttonAuthenticate.Enabled = true;
            }
        }