首页 > 代码库 > asp.net mvc5取得post过来的数据

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 =http://www.mamicode.com/ 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();        }    }}
View Code

 

 

 

结果:

技术分享

asp.net mvc5取得post过来的数据