C#使用PostAsync进行多次请求时,只有首次发送有请求体,后续发送接收方均检测没有请求体而报错。
private async void TwoNetSend(string objectURL, string sendData)
{
string URL = "";
string requestURI = "";
bool bEnable = GetTwoNetParas(out URL, out requestURI);
//bool bEnable = true;
if (bEnable == false)
{
Noter.Write("未配置传输参数", "TwoNetSend");
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);
using (StringContent postContent = new StringContent(pc, Encoding.GetEncoding("GBK"), "application/json"))
{
Noter.Write("传输数据:" + pc, "TwoNetSend");
var response = await _Client.PostAsync(requestURI, postContent);
bool result = response.IsSuccessStatusCode;
Noter.Write("传输状态:" + result.ToString(), "TwoNetSend");
}
_Client.Dispose();
}
catch (Exception ex)
{
Noter.Write(ex.ToString(), "TwoNetSend");
return;
}
}