c# - 通过 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;
}
}
解决方案
尝试使用JSON.NET在代码中序列化您的 objekt,如下所示:
string json = JsonConvert.SerializeObject(<your object>);
推荐阅读
- javascript - AWS JS SDK TypeError:STS 不是构造函数
- php - 如何将数据插入到golang中的多维数组中?
- javascript - Javascript / React 中的动态配置变量
- amp-html - 是否可以使用 amp-story 调用 API?有没有例子?
- javascript - 使用服务 Angular 在组件之间传递数据
- javascript - 将 webglearth 画布的输出绘制到 2d 画布
- css - 选择框选项上的文本转换
- rust - 在 C 库的回调函数中使用类型参数的引用
- dart - Flutter:共享偏好
- asp.net-mvc - 使用 mvc 应用程序安装 Apache?