Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析通知.net UTF-8格式_C#_Android_.net_Web Services_Parse Platform - Fatal编程技术网

C# 解析通知.net UTF-8格式

C# 解析通知.net UTF-8格式,c#,android,.net,web-services,parse-platform,C#,Android,.net,Web Services,Parse Platform,我正在为android的PushNotification使用Parse.NETAPI。我的Webservice在这里 private bool PushNotification(string pushMessage) { bool isPushMessageSend = false; string postString = ""; string urlpath = "https://api.parse.com/1/push"; var httpWebReques

我正在为android的
PushNotification
使用Parse.NETAPI。我的
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()
我怎样才能解决这个问题

感谢您的帮助

我们开始:

httpWebRequest.ContentLength = postString.Length;
您假设每个字符都是一个字节,但情况并非总是如此。您可以通过
编码计算UTF-8长度。GetByteCount
-使用它

httpWebRequest.ContentLength = myEncopding.GetByteCount(postString);
或者更好:预先计算有效负载:

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

我使用以下代码解决了这个问题:

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

“错误发生”并没有告诉我们您是从哪里得到错误的,或者错误是什么样子的。始终提供有关您收到的任何错误的信息。(此外,我们不知道web服务器使用的是什么编码。)错误发生在哪里?有调用堆栈吗?@matt i add error对这个错误表示抱歉,请注意:您可能需要使用实际的json序列化程序/格式化程序,或者至少添加一些转义。