标签:
可以放在控制器的入口函数中,这样载入就会被调用
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(); } } }
结果:
标签:
原文地址:http://www.cnblogs.com/hont/p/4249963.html