通过HttpWebRequest在Json内部传递符号

时间:2018-11-13 09:57:46

标签: c# json httpwebrequest symbols

我尝试使用以下方法以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;
        }

    }

1 个答案:

答案 0 :(得分:1)

尝试使用docs在如下代码中序列化对象:

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