错误的请求发送gcm信息

问题描述:

我想通过C#发送gcm信息。错误的请求发送gcm信息

我尝试了几次,但是我试着用json方法发送它时得到了http:400-Bad request

当我尝试在文本中发送它时,我无法读取它(rtl语言) - 这就是为什么我尝试JSON

任何人都知道什么问题? 谢谢!

 private static string SendNotificationJson2(string id, string msg) 
    { 
      var AuthString = "AIzaSyDAtmaqSdutBQemqmd4dQgf33B_6ssbvXA"; 
      var RegistrationID = id; 
      var Message = msg; 


     //-- Create GCM request insted of C2DM Web Request Object --// 
     HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send"); 

     Request.Method = "POST"; 
     Request.KeepAlive = false; 

     //-- Create Query String --// 
     Dictionary<String, String> dict = new Dictionary<string, string>(); 
     dict.Add("registration_ids", RegistrationID); 
     dict.Add("data", Message); 
     dict.Add("collapse_key", "1"); 

     string postData = GetPostStringFrom(dict); 
     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     Request.ContentType = "application/json"; 
     Request.ContentLength = byteArray.Length; 


     Request.Headers.Add("Authorization", "key=" + AuthString); 

     //-- Delegate Modeling to Validate Server Certificate --// 
     ServicePointManager.ServerCertificateValidationCallback += delegate(
        object 
        sender, 
        System.Security.Cryptography.X509Certificates.X509Certificate 
        pCertificate, 
        System.Security.Cryptography.X509Certificates.X509Chain pChain, 
        System.Net.Security.SslPolicyErrors pSSLPolicyErrors) 
     { 
      return true; 
     }; 

     //-- Create Stream to Write Byte Array --// 
     Stream dataStream = Request.GetRequestStream(); 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Close(); 

     //-- Post a Message --// 
     WebResponse Response = Request.GetResponse(); 
     HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode; 
     if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden)) 
     { 
      return "Unauthorized - need new token"; 

     } 
     else if (!ResponseCode.Equals(HttpStatusCode.OK)) 
     { 
      return "Response from web service isn't OK"; 
      //Console.WriteLine("Response from web service not OK :"); 
      //Console.WriteLine(((HttpWebResponse)Response).StatusDescription); 
     } 

     StreamReader Reader = new StreamReader(Response.GetResponseStream()); 
     string responseLine = Reader.ReadLine(); 
     Reader.Close(); 

     return "ok"; 
    } 

    private static string GetPostStringFrom(Dictionary<string,string> postFieldNameValue) 
    { 
     // return Newtonsoft.Json.JsonConvert.SerializeObject(postFieldNameValue); 
     return "\"data\": {\"Message\": \"" + postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" + postFieldNameValue["registration_ids"] + "\"]}"; 
    }</code> 

你的注册ID必须是一个JSON阵列,使得使用一个列表,而不是单根绳制造的。

你忘了第一架在JSON数据使用

return "{\"data\": {\"Message\": \"" + postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" + postFieldNameValue["registration_ids"] + "\"]}"; 

代替

return "\"data\": {\"Message\": \"" + postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" + postFieldNameValue["registration_ids"] + "\"]}";