Click here to Skip to main content
15,890,690 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to accelebrate chart when have many series? Pin
smallkubi26-Nov-15 5:16
smallkubi26-Nov-15 5:16 
GeneralRe: how to accelebrate chart when have many series? Pin
smallkubi27-Nov-15 21:59
smallkubi27-Nov-15 21:59 
AnswerRe: how to accelebrate chart when have many series? Pin
Richard Deeming26-Nov-15 1:57
mveRichard Deeming26-Nov-15 1:57 
GeneralRe: how to accelebrate chart when have many series? Pin
John Torjo26-Nov-15 3:35
professionalJohn Torjo26-Nov-15 3:35 
GeneralRe: how to accelebrate chart when have many series? Pin
Richard Deeming26-Nov-15 3:40
mveRichard Deeming26-Nov-15 3:40 
GeneralRe: how to accelebrate chart when have many series? Pin
John Torjo26-Nov-15 3:41
professionalJohn Torjo26-Nov-15 3:41 
GeneralRe: how to accelebrate chart when have many series? Pin
Pete O'Hanlon26-Nov-15 5:16
mvePete O'Hanlon26-Nov-15 5:16 
QuestionHttpWebRequest in WCF Pin
Giorgi Martiashvili24-Nov-15 20:00
Giorgi Martiashvili24-Nov-15 20:00 
C#
public class GcmSender
    {
        private const string GcmUri = "https://android.googleapis.com/gcm/send";

        #region Properties

        public string DeviceToken { get; set; }
        public string ApiKey { get; set; }
        public string ResultJson { get; private set; }

        #endregion

        HttpWebRequest _gcmRequest;
        HttpWebResponse _gcmResponse;

        public GcmSender()
        {
            // do nothing
        }

        public GcmSender(string deviceToken, string apiKey)
        {
            DeviceToken = deviceToken;
            ApiKey = apiKey;
        }

        public string Send(string message)
        {
            // Escape condition
            if (DeviceToken == null || ApiKey == null)
            {
                return "[ERROR] Device Token or API Key has not been set";
            }

            InitGcmClient();
            PostPayload(message);

            try
            {
                _gcmResponse = _gcmRequest.GetResponse() as HttpWebResponse;
            }
            catch (WebException we)
            {
                return "[ERROR] There is a problem within processing GCM message \n" + we.Message;
            }
            ResultJson = ReadResponse(_gcmResponse);

            return ResultJson;
        }

        private string ReadResponse(HttpWebResponse response)
        {
            StreamReader responseReader = new StreamReader(response.GetResponseStream());
            return responseReader.ReadToEnd();
        }

        private void InitGcmClient()
        {
            _gcmRequest = WebRequest.Create(GcmUri) as HttpWebRequest;
            if (_gcmRequest != null)
            {
                _gcmRequest.ContentType = "application/json";
                _gcmRequest.UserAgent = "Android GCM Message Sender Client 1.0";
                _gcmRequest.Method = "POST";

                // Credential info
                _gcmRequest.Headers.Add("Authorization", "key=" + ApiKey);
            }
        }

        private void PostPayload(string message)
        {
            // Ready
            string payloadString = AssembleJSONPayload(DeviceToken, message);
            byte[] payloadByte = Encoding.UTF8.GetBytes(payloadString);
            _gcmRequest.ContentLength = payloadByte.Length;

            // Go
            using (Stream payloadStream = _gcmRequest.GetRequestStream())
            {
                payloadStream.Write(payloadByte, 0, payloadByte.Length);
                payloadStream.Close();
            }
        }

        private string AssembleJSONPayload(string gcmDeviceToken, string gcmBody)
        {
            string payloadFormatJSON =
                "{{" +
                    "\"registration_ids\" : [\"{0}\"]," +
                    "\"data\" : {{" +
                        " {1} " +
                    "}}" +
                "}}";

            string payload = string.Format(payloadFormatJSON, gcmDeviceToken, gcmBody);
            Debug.WriteLine("payload : " + payload);

            return payload;
        }
    }




This code works fine in WPF application. when I try this code in wcf it crashes at
_gcmResponse = _gcmRequest.GetResponse() as HttpWebResponse;
and says
The remote server returned an error: (400) Bad Request.
. Can anyone help me?
SuggestionRe: HttpWebRequest in WCF Pin
Kornfeld Eliyahu Peter24-Nov-15 22:49
professionalKornfeld Eliyahu Peter24-Nov-15 22:49 
AnswerRe: HttpWebRequest in WCF Pin
Rob Philpott25-Nov-15 1:10
Rob Philpott25-Nov-15 1:10 
Question(WPF) How to pass events from child to parent Pin
Foothill24-Nov-15 10:51
professionalFoothill24-Nov-15 10:51 
Generalcoding problem progress bar Pin
Kris Saelen24-Nov-15 5:56
Kris Saelen24-Nov-15 5:56 
AnswerRe: coding problem progress bar Pin
Duncan Edwards Jones24-Nov-15 5:57
professionalDuncan Edwards Jones24-Nov-15 5:57 
GeneralRe: coding problem progress bar Pin
Eddy Vluggen24-Nov-15 6:00
professionalEddy Vluggen24-Nov-15 6:00 
GeneralRe: coding problem progress bar Pin
R. Giskard Reventlov24-Nov-15 6:51
R. Giskard Reventlov24-Nov-15 6:51 
GeneralRe: coding problem progress bar Pin
Ian Shlasko24-Nov-15 7:14
Ian Shlasko24-Nov-15 7:14 
AnswerRe: coding problem progress bar Pin
John Torjo24-Nov-15 11:33
professionalJohn Torjo24-Nov-15 11:33 
GeneralRe: coding problem progress bar Pin
Kris Saelen28-Nov-15 10:50
Kris Saelen28-Nov-15 10:50 
QuestionRead, write and processing each row of a gridview ? Pin
Member 245846723-Nov-15 15:59
Member 245846723-Nov-15 15:59 
AnswerRe: Read, write and processing each row of a gridview ? Pin
F-ES Sitecore23-Nov-15 22:44
professionalF-ES Sitecore23-Nov-15 22:44 
AnswerRe: Read, write and processing each row of a gridview ? Pin
John Torjo24-Nov-15 0:20
professionalJohn Torjo24-Nov-15 0:20 
AnswerRe: Read, write and processing each row of a gridview ? Pin
Simon_Whale24-Nov-15 5:02
Simon_Whale24-Nov-15 5:02 
GeneralRe: Read, write and processing each row of a gridview ? Pin
Member 245846724-Nov-15 18:58
Member 245846724-Nov-15 18:58 
GeneralRe: Read, write and processing each row of a gridview ? Pin
Simon_Whale24-Nov-15 22:00
Simon_Whale24-Nov-15 22:00 
GeneralRe: Read, write and processing each row of a gridview ? Pin
Member 245846724-Nov-15 22:49
Member 245846724-Nov-15 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.