c# - Parse Notification .net UTF-8 format -
i using parse .net api pushnotification
android. webservice
here
private bool pushnotification(string pushmessage) { bool ispushmessagesend = false; string poststring = ""; string urlpath = "https://api.parse.com/1/push"; var httpwebrequest = (httpwebrequest)webrequest.create(urlpath); poststring = "{ \"channels\": [ \"trials\" ], " + "\"data\" : {\"alert\":\"" + pushmessage + "\"}" + "}"; httpwebrequest.contenttype = "application/json"; httpwebrequest.contentlength = poststring.length; httpwebrequest.headers.add("x-parse-application-id", "my parse app id"); httpwebrequest.headers.add("x-parse-rest-api-key", "my rest api key"); httpwebrequest.method = "post"; streamwriter requestwriter = new streamwriter(httpwebrequest.getrequeststream()); requestwriter.write(poststring); requestwriter.close(); var httpresponse = (httpwebresponse)httpwebrequest.getresponse(); using (var streamreader = new streamreader(httpresponse.getresponsestream())) { var responsetext = streamreader.readtoend(); jobject jobjres = jobject.parse(responsetext); if (convert.tostring(jobjres).indexof("true") != -1) { ispushmessagesend = true; } } return ispushmessagesend; }
this code works correctly english alphabet. when try use letter "ü","ş","ö","ç" etc. error occurred.
error here
system.net.webexception: request aborted: request canceled. ---> system.io.ioexception: cannot close stream until bytes written. @ system.net.connectstream.closeinternal(boolean internalcall, boolean aborting) --- end of inner exception stack trace - @ system.net.connectstream.closeinternal(boolean internalcall, boolean aborting) @ system.net.connectstream.system.net.icloseex.closeex(closeexstate closestate) @ system.net.connectstream.dispose(boolean disposing) @ system.io.stream.close() @ system.io.streamwriter.dispose(boolean disposing) @ system.io.streamwriter.close()
how can solve problem.
thanks
here go:
httpwebrequest.contentlength = poststring.length;
you have assumed each character 1 byte, not case. can calculate utf-8 length via encoding.getbytecount(s)
- use instead.
httpwebrequest.contentlength = myencopding.getbytecount(poststring);
or better: pre-compute payload:
var data = myencopding.getbytes(poststring); httpwebrequest.contentlength = data.length; //... requeststream.write(data, 0, data.length);
Comments
Post a Comment