我想通過HTTP請求發送一個xml字符串,並在另一端接收它。在接收端,我總是得到這個XML爲空。你能告訴我爲什麼嗎?如何通過HTTP請求發送xml,並使用ASP.NET MVC接收它?
發送:
var url = "http://website.com";
var postData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xml>...</xml>";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
var req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "text/xml";
req.Method = "POST";
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
string response = "";
using (System.Net.WebResponse resp = req.GetResponse())
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
response = sr.ReadToEnd().Trim();
}
}
接收:
[HttpPost]
[ValidateInput(false)]
public ActionResult Index(string xml)
{
//xml is always null
...
return View(model);
}
似乎有點醜,但對我來說與MVC工作太4. – AFract
感謝,也爲我工作.. 。如果您是發佈它,你要讀一個流輸入流來獲取數據。要以「xml」可接受的方式接收,您必須將此查詢字符串作爲post參數發送 –