POST Body的XML中的字符未被转义

时间:2017-05-16 21:47:23

标签: asp.net http post webrequest

我试图在POST请求中将一个xml流发送到REST端点,如此...

var request = (HttpWebRequest)WebRequest.Create(xmlUrl);
var stringData = System.IO.File.ReadAllText(xmlForm);
var data = Encoding.UTF8.GetBytes("xform=" + stringData);
request.Method = "POST";
request.KeepAlive = true;
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/5.0";
request.Headers.Add("Accept-Language:en-CA,*");
var newStream = request.GetRequestStream();
await newStream.WriteAsync(data, 0, data.Length);
var response = (HttpWebResponse)request.GetResponse(); 

我遇到的问题是,当我检查Fiddler时,身体是碎片的,因为& amp;& amp;& amp;价值没有被转义,我不知道如何解决这个问题。

身体最终碎片化......

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现了问题:

 var request = (HttpWebRequest)WebRequest.Create(xmlUrl);
 var stringData = System.IO.File.ReadAllText(xmlForm);

 stringData = "xform=" + HttpUtility.UrlEncode(stringData);

 var data = Encoding.UTF8.GetBytes(stringData);
 request.Method = "POST";
 request.KeepAlive = true;
 request.Headers.Add("Accept-Encoding", "gzip, deflate");
 request.ContentType = "application/x-www-form-urlencoded"; 
 request.ContentLength = data.Length;
 request.UserAgent = "Mozilla/5.0";
 request.Headers.Add("Accept-Language:en-CA,*");
 var newStream = request.GetRequestStream();
 await newStream.WriteAsync(data, 0, data.Length);
 var response = (HttpWebResponse)request.GetResponse(); 

问题在于我需要对数据进行UrlEncode以便不对其进行解释。