雷璐 2023-07-26 19:44 采纳率: 0%
浏览 21

C#,使用httpclient发送异步post请求,出现没有请求体的情况

在使用C#进行多次异步post请求的时候,只有首次发送有请求体,后续发送接收方均检测没有请求体而报错。不知道是什么原因,请教一下大家,使用的标准的post异步请求方式。
具体代码,

private void TwoNetSend(string objectURL, string sendData)
        {
            string URL = "";
            string requestURI = "";
            bool bEnable = GetTwoNetParas(out URL, out requestURI);
            //bool bEnable = true;
            if (bEnable == false)
            {
                return;
            }
            try
            {
                HttpClient _Client = new HttpClient();
                _Client.BaseAddress = new Uri(URL);
                _Client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                Dictionary<string, string> paras = new Dictionary<string, string>();
                paras.Add("URL", objectURL);
                paras.Add("sendData", sendData);
                string pc = JsonConvert.SerializeObject(paras);
                StringContent postContent = new StringContent(pc, Encoding.GetEncoding("GBK"), "application/json");

                var response = _Client.PostAsync(requestURI, postContent).Result;
                bool result = response.IsSuccessStatusCode;

                _Client.Dispose();
            }
            catch (Exception ex)
            {
                Noter.Write(ex.ToString(), "TwoNetSend");
                return;
            }
        }

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-07-26 19:50
    关注

    代码没有贴,不能判断啊
    请求体要你自己写 requestStream.Write(postData)

    评论

报告相同问题?

问题事件

  • 修改了问题 7月26日
  • 创建了问题 7月26日