Asp.Net Get请求如何将参数放入信息体body中

  Asp.Net Get请求如何将参数放入信息体http body中,如下图Fiddler抓包所示

Asp.Net Get请求如何将参数放入信息体body中

  可以使用反射来设置ContentBodyNotAllowed实现,如果直接get请求,调用request.GetRequestStream()会出错。

Asp.Net Get请求如何将参数放入信息体body中

Asp.Net Get请求如何将参数放入信息体http body中代码如下

using System.Net;
using System.IO;
using System.Reflection;
namespace Test.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult test() {
            var request = WebRequest.Create("http://www.w3dev.cn");
            request.ContentType = "application/json";
            request.Method = "GET";
            request.ServicePoint.Expect100Continue = false;//取消Expect:100-continue请求头,要不服务器没有正确处理会报错,如IIS。远程服务器返回错误: (417) Expectation Failed。
            var type = request.GetType();
            var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(request);
            var methodType = currentMethod.GetType();
            methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write("{fid:123}");
            }
            var response = (HttpWebResponse)request.GetResponse();
            return Content("");
        }
}

代码来源:https://stackoverflow.com/questions/43421126/how-to-use-httpclient-to-send-content-in-body-of-get-request

加支付宝好友偷能量挖...


原创文章,转载请注明出处:Asp.Net Get请求如何将参数放入信息体body中

评论(0)Web开发网
阅读(661)喜欢(0)Asp.Net/C#/WCF