asp.net mvc5取得post过来的数据
可以放在控制器的入口函数中,这样载入就会被调用
public ActionResult Index() { Stream s = Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); var postStr = Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) { Response.Write("Response!"); } return null; }
从request中读入流,转出string.
客户端模拟发送:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var targetWeb = "http://localhost:60797/"; var reqContent = "My Content"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(targetWeb); req.Method = "POST"; byte[] data = Encoding.UTF8.GetBytes(reqContent); req.ContentLength = data.Length; Stream stream = req.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); WebResponse resp = req.GetResponse(); stream = resp.GetResponseStream(); StreamReader reader = new StreamReader(stream); string response = reader.ReadToEnd(); req.Abort(); resp.Close(); Console.WriteLine("发出请求: " + reqContent + " 目标地址: " + targetWeb); Console.WriteLine("返回响应: " + response); Console.Read(); } } }
结果:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。