在吉特日化MES调用某公司AGV平台下发任务的时候,使用HttpClient 进行POST请求,出现如下异常: HttpClient基础连接已经关闭: 连接被意外关闭 , 之前已经使用HTTPClient做过大量的三方请求均为发现此问题
public string Execute(string ApiName, JObject parameters)
{
string result = string.Empty;
try
{
string BaseUrl = this.BaseUrl;
string ApiUrl = string.Format("{0}{1}", BaseUrl, ApiName);
HttpContent httpContent = new StringContent(parameters.ToString(), Encoding.UTF8, "text/json");
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();
string responseJson = httpClient.PostAsync(ApiUrl, httpContent).Result.Content.ReadAsStringAsync().Result;
return responseJson;
}
catch (Exception e)
{
DataResult dataResult = new DataResult() { Code = (int)EResponseCode.Exception, Message = e.Message };
result = JsonHelper.SerializeObject(dataResult);
}
return result;
}
通过如下方式解决,在配置文件中新增如下配置:
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>