c# - Error 400 GCM Invalid Request json -


i new kind of programming don't know if there answer already, weren't able find it. testing see if can dry-run gcm message work without errors.

the error error 400 invalid request, , it's saying json being invalid, have assumed problem has string manipulation or definition of postdata, can't figure out. of code copy pasted anyway 1 believe others in similar situation same error, if copy same source.

and have put in actual values "lorem"s.

this code:

using system; using system.collections.generic; using system.text; using system.net; using system.io; using system.web.script.serialization;  namespace servergmc {     public class servergmc     {         static void main ()         {             // prepares , calls function send message             list<string> redidlist = new list<string>(1) { "aaaaaaaaaaaaaaaaaaaaaaaa" };             redidlist.trimexcess();             console.writeline(sendnotification(redidlist, "helloworld", "test", 220299));             console.read();         }         static public string sendnotification(list<string> deviceregids, string message, string title, long id)         {             try             {                 string regids = string.join("\",\"", deviceregids);                  string appid = "lorem";                 var senderid = "lorem";                  notificationmessage nm = new notificationmessage();                 nm.title = title;                 nm.message = message;                 nm.itemid = id;                  var value = new javascriptserializer().serialize(nm);                 webrequest wrequest;                 wrequest = webrequest.create("https://android.googleapis.com/gcm/send");                 wrequest.method = "post";                 wrequest.contenttype = " application/json;charset=utf-8";                 wrequest.headers.add(string.format("authorization: key={0}", appid));                 wrequest.headers.add(string.format("sender: id={0}", senderid));                  string postdata = "{\"collapse_key\":\"standard\",\"time_to_live\":108,\"delay_while_idle\":true,\"dry_run\":true,\"data\": { \"message\" : " + "\"" + value + "\",\"time\": " + "\"" + system.datetime.now.tostring() + "\"},\"registration_ids\":[\"" + regids + "\"]}";                 //string postdata = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&date.time=" + system.datetime.now.tostring() + "&registration_ids=" + regids + "";                 console.writeline(postdata);                  byte[] bytes = encoding.utf8.getbytes(postdata);                 wrequest.contentlength = bytes.length;                  stream stream = wrequest.getrequeststream();                 stream.write(bytes, 0, bytes.length);                 stream.close();                  webresponse wresponse = wrequest.getresponse();                  stream = wresponse.getresponsestream();                  streamreader reader = new streamreader(stream);                  string response = reader.readtoend();                  httpwebresponse httpresponse = (httpwebresponse)wresponse;                 string status = httpresponse.statuscode.tostring();                  reader.close();                 stream.close();                 wresponse.close();                  if (status == "")                 {                     return response;                 }                 else                 {                     return "";                 }             }             catch (exception e)             {                 console.writeline(e.tostring());                 console.writeline();                 return "";             }         }         private class notificationmessage         {             public string title;             public string message;             public long itemid;         }     } } 

the postdata isn't formatted in json. if check out using online formatting tool, looks this

{     "collapse_key":"standard",     "time_to_live":108,     "delay_while_idle":true,     "dry_run":‌​true,     "data":{         "message":"{"title":"test",         "message":"helloworld",         "itemid":220299}",         "time":"22/04/2016 13:04:38"     },     "registration_ids":["aaaaaaaaaaaaaaaaaaaaaaaa"] } 

you can either remove data.message node , place properties in data, or use 3rd-party json parser or system.web.helpers.json.decode (which suggested in this issue)

hopefully helps issue.

happy coding!


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -