转义从我的API方法传入的转义字符

本文关键字:转义字符 方法 我的 API 转义 | 更新日期: 2023-09-27 18:03:06

生成xml的API方法如下(这很好)

"<?xml version='"1.0'" encoding='"UTF-8'"?>
在我的API方法 之间
 return Request.CreateResponse(HttpStatusCode.OK, res);

以及从

获取响应的位置
string api_response = client.UploadString(myurl, myRequest)

上面的api_response字符串看起来像下面的

"'"<?xml version='''"1.0'''" encoding='''"UTF-8'''"?>

导致我的XDocument。解析失败

编辑:更多细节我通过这个代码 得到响应
  HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.Accept = "text/xml";
            httpRequest.Credentials = new NetworkCredential(userName, password);
            byte[] bytesToWrite = Encoding.UTF8.GetBytes(root.ToString());
            using (Stream st = httpRequest.GetRequestStream())
            {
                st.Write(bytesToWrite, 0, bytesToWrite.Length);
            }
            HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
            string read = reader.ReadToEnd();
            return read;

read返回格式良好的xml:只有在我创建响应后,当额外的反斜杠开始发挥作用(看着字符串单击放大镜选项)

当我从我的api返回响应时(作为internet explorer的文本),我的实际文本是这样的"

figure it out将my替换为ResponseMessage

var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Accepted) {
      RequestMessage = Request,
      Content = new StringContent(content)
    };
    return ResponseMessage(httpResponseMessage);

转义从我的API方法传入的转义字符

可以更改

 return Request.CreateResponse(HttpStatusCode.OK, res);

var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Accepted) {
      RequestMessage = Request,
      Content = new StringContent(content)
    };
    return ResponseMessage(httpResponseMessage);