title: GCM Server ( The remote server returned an error: (401) Unauthorized.)
body: <p>I have been working over GCM Server in .net but I am unable to post data to server and always getting this error .</p>
  
  <p>The remote server returned an error: (401) Unauthorized.</p>
  
  <p>The asp.net code is mentioned below.</p>
  
  <pre><code>        string regId = "xxxx1bER6XXjuEhxxxxxxxxxxxxxxxxxxxxxf_thTnR7CSxxxxxxqX60-jxxxN55xxxxK074AaV9M09jP2xxxxu28aXXrd6ltxxxxxxhvmVt-nxxfw";
  
  
            var applicationID = "AIzaxxxxxt6fJx5txxxxxx";
  
  
  
              var SENDER_ID = "x8xxxxxx030x";
              var value = Text1.Text;
              WebRequest tRequest;
              tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
              tRequest.UseDefaultCredentials = true;
              tRequest.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
              tRequest.Method = "post";
              tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
              tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
  
              tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
  
               //Data_Post Format
               // string postData = "{'collapse_key' : 'demo', 'registration_id': [ '" + regId + "' ], 
                //'data': {'message': '" + Label1.Text + "'},'time_to_live' : '3' }";
  
  
              string postData = "collapse_key=score_update&amp;time_to_live=108&amp;delay_while_idle=1&amp;data.message="
                  + value + "&amp;data.time=" + System.DateTime.Now.ToString() + "&amp;registration_id=" + regId + "";
  
  
              Console.WriteLine(postData);
              Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
              tRequest.ContentLength = byteArray.Length;
  
              Stream dataStream = tRequest.GetRequestStream();
              dataStream.Write(byteArray, 0, byteArray.Length);
              dataStream.Close();
  
              WebResponse tResponse = tRequest.GetResponse();
  
              dataStream = tResponse.GetResponseStream();
  
              StreamReader tReader = new StreamReader(dataStream);
  
              String sResponseFromServer = tReader.ReadToEnd();
  
              Label3.Text = sResponseFromServer; //printing response from GCM server.
              tReader.Close();
              dataStream.Close();
              tResponse.Close();
  </code></pre>
  
  <p>I have entered right credentials but still the error exists . I am stuck with this scenario for two days now , please help me regarding GCM Server in .NET WCF . Thanks</p>