首页 > 解决方案 > 通过 HttpWebRequest 在 Json 中传递符号

问题描述

我尝试使用以下方法以 Json 格式将消息传递给 MS Flow,但是一旦传递任何符号(如“),我就会收到错误,因为符号被识别为代码。

public static bool notification(string customer, string comment)
    {
        try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("my msflow link goes here");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{ \"customer\":\"" + customer + "\",\"comment\":\"" + comment + "\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();

            }

            return true;
        }
        catch (Exception)
        {
            return false;
        }

    }

标签: c#jsonhttpwebrequestsymbols

解决方案


尝试使用JSON.NET在代码中序列化您的 objekt,如下所示:

string json = JsonConvert.SerializeObject(<your object>);

推荐阅读