我正在使用HttpWebRequest从web服务中检索HttpWebResponse.GetResponseStream数据。Web API
我的内容类型是: request.ContentType =“应用程序/json”;
问题是,当我检索文本时,信封中的所有内容都会被转换。所以<>是<和>,等等。
如何检索数据,并保留有效XML的小于/大于符号?
谢谢!
replace方法转换我不想要的xml <>信封之间的所有特殊字符
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json";
//request.ContentType = "text/xml;charset=utf-8";
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Method = HttpVerb;
request.Accept = "application/xml";
if (ObjData != null)
{
var Serialized = JsonConvert.SerializeObject(ObjData);
using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
{
sw.Write(Serialized);
}
}
else
{
request.ContentLength = 0;
}
HttpWebResponse httpWebResponse = request.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
if (httpWebResponse.StatusCode == HttpStatusCode.Unauthorized || httpWebResponse.StatusCode == HttpStatusCode.InternalServerError || httpWebResponse.StatusCode == HttpStatusCode.NotAcceptable)
{
strResponse = httpWebResponse.StatusDescription;
}
else if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{
strResponse = String.Format("POST failed. Received HTTP {0}", httpWebResponse.StatusCode);
}
else
{
strResponse = sr.ReadToEnd();// this line creating the wrong xml
strResponse = XMLDecode(strResponse);
}
}发布于 2019-03-28 22:21:29
这3行代码解决了我的问题:
strResponse = sr.ReadToEnd();//在这里它使用< etc转换<图表时出现问题
XmlDocument xmlDocument = new xml ();//这3行代码解决了XmlDocument的问题
xmlDocument.LoadXml(strResponse);
strResponse = xmlDocument.InnerText;
https://stackoverflow.com/questions/55397749
复制相似问题