首页 > 代码库 > 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(); } }}
结果:
asp.net mvc5取得post过来的数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。