简体   繁体   中英

Parse Notification .net UTF-8 format

I am using Parse .Net api for PushNotification for android. My Webservice is 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 for English alphabet. But when i try to use letter like "ü","ş","ö","ç" etc. Error occurred.

Error is here

System.Net.WebException: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  --- End of inner exception stack trace -
  at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
  at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
  at System.Net.ConnectStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at System.IO.StreamWriter.Dispose(Boolean disposing)
  at System.IO.StreamWriter.Close()

How can i solve this problem.

Thanks for help

Here we go:

httpWebRequest.ContentLength = postString.Length;

You have assumed that each character is one byte, which is not always the case. You can calculate the UTF-8 length via Encoding.GetByteCount(s) - use that instead.

httpWebRequest.ContentLength = myEncopding.GetByteCount(postString);

Or even better: pre-compute the payload:

var data = myEncopding.GetBytes(postString);
httpWebRequest.ContentLength = data.Length;
//...
requestStream.Write(data, 0, data.Length);

I solved using the following code:

public bool SendPushNotification(string jsonContent)
{
    string urlpath = "https://api.parse.com/1/push";
    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(urlpath);
    request.Method = "POST";
    string appId = "...";
    string restApiKey = "...";

    request.Headers.Add("X-Parse-Application-Id", appId);
    request.Headers.Add("X-Parse-REST-API-KEY", restApiKey);

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes(jsonContent);

    request.ContentLength = byteArray.Length;
    request.ContentType = @"application/json";

    using (Stream dataStream = request.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }

    try
    {
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            long length = response.ContentLength;
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                JObject jObjRes = JObject.Parse(responseText);
                if (Convert.ToString(jObjRes).IndexOf("true") != -1)
                {
                    return true;
                }
            }
        }
    }
    catch (WebException ex)
    {
        // Log exception and throw as for GET example above
        return false;
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM