【问题标题】:Parse Notification .net UTF-8 format解析通知 .net UTF-8 格式
【发布时间】:2014-06-09 12:40:48
【问题描述】:

我正在使用 Parse .Net api for PushNotification for android。我的Webservice在这里

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;
}

此代码适用于英文字母。但是当我尝试使用“ü”、“ş”、“ö”、“ç”等字母时,发生了错误。

错误在这里

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()

我该如何解决这个问题。

感谢帮助

【问题讨论】:

  • "error occurred" 没有向我们提供任何关于您在哪里遇到错误或错误的信息。 始终提供有关您收到的任何错误的信息。 (此外,我们不知道 Web 服务器使用的是什么编码。)
  • 错误发生在哪里?任何调用堆栈?
  • @matt 我添加错误对不起这个错误
  • 附带说明:您可能想要使用实际的 json 序列化程序/格式化程序 - 或者至少添加一些转义。

标签: c# android .net web-services parse-platform


【解决方案1】:

我们开始吧:

httpWebRequest.ContentLength = postString.Length;

您假设每个字符都是一个字节,但情况并非总是如此。您可以通过 Encoding.GetByteCount(s) 计算 UTF-8 长度 - 请改用它。

httpWebRequest.ContentLength = myEncopding.GetByteCount(postString);

甚至更好:预先计算有效载荷:

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

【讨论】:

    【解决方案2】:

    我使用以下代码解决了:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2011-12-12
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2018-02-07
      • 2016-05-11
      相关资源
      最近更新 更多