楚河之界 2022-07-13 13:58 采纳率: 100%
浏览 51
已结题

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

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

img

直接get请求,调用request.GetRequestStream()时会出错

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-07-13 14:00
    关注

    可以使用反射来设置ContentBodyNotAllowed实现,示例代码如下

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月22日
  • 已采纳回答 7月14日
  • 专家修改了标签 7月13日
  • 创建了问题 7月13日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改