调restfulAPI返回json中文转义

时间:2021-09-16
本文章向大家介绍调restfulAPI返回json中文转义,主要包括调restfulAPI返回json中文转义使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

/// <summary>
/// 调用OCR API
/// </summary>
/// <param name="img">图片base64</param>
/// <returns></returns>
public static string Api(string img, string imgName)
{
string val = "";
try
{
//string url = "http://10.10.251.100:9202/inference/afbd56e1-cdee-4b7b-9a66-71d5967570f8/raw/sync?count=1";
string url = System.Configuration.ConfigurationManager.AppSettings["url"];
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/text; charset=utf-8"; // "application/json";
string data = "{\"inferenceData\":[{\"imageId\":\"" + imgName + "\",\"imageContent\":\"" + img + "\"}]}";
//路径的不行
//string data = "{\"inferenceData\":[{\"imageId\":\"" + imgName + "\",\"imageURL\":\"" + imgUrl + "\"}]}";

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
val = Regex.Unescape(reader.ReadToEnd());
}
}
catch (Exception ex)
{

}
return val;
}

以上在没有使用Regex.Unescape前,获取返回的数据中文呈现都是转义的字符:

使用Regex.Unescape之后:

原文地址:https://www.cnblogs.com/ang-1314/p/15292912.html