码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-01-26 13:15:24      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

可以放在控制器的入口函数中,这样载入就会被调用

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();
        }
    }
}
View Code

 

 

 

结果:

技术分享

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

标签:

原文地址:http://www.cnblogs.com/hont/p/4249963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!