C# System.Net.Http.HttpRequestException:将内容复制到流时出错---&燃气轮机;System.IO.IOException:响应过早结束
我使用以下代码来获取一些http api的调用C# System.Net.Http.HttpRequestException:将内容复制到流时出错---&燃气轮机;System.IO.IOException:响应过早结束,c#,httprequest,httpclient,.net-core-3.1,C#,Httprequest,Httpclient,.net Core 3.1,我使用以下代码来获取一些http api的调用 public static async Task<GenerricApiResponse> ProcessWebRequest<T>(string url, T req,ILog Logger, int timeoutInSeconds = 0) { var obj = new GenerricApiResponse(); try { using
public static async Task<GenerricApiResponse> ProcessWebRequest<T>(string url, T req,ILog Logger, int timeoutInSeconds = 0)
{
var obj = new GenerricApiResponse();
try
{
using (var client = new HttpClient())
{
var jsonRequest = JsonSerializer.Serialize(req);
client.DefaultRequestHeaders.ExpectContinue = false;
var content = new StringContent(jsonRequest);
content.Headers.Clear();
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
if (timeoutInSeconds > 0)
{
client.Timeout = new TimeSpan(0, 0, timeoutInSeconds);
}
var response = await client.PostAsync(url, content);
obj.HttpResponseCode = response.StatusCode;
try
{
string responsecontent = null;
responsecontent = response.Content.ReadAsStringAsync().Result;
if (response.Content != null && response.Content.Headers != null)
{
obj.ResponseContentType = response.Content.Headers.ContentType.MediaType;
if (responsecontent != null && obj.ResponseContentType == "text/html")
{
if (responsecontent != null && responsecontent.Length > 1000)
{
responsecontent = responsecontent.Substring(0, 1000) + "...";
}
}
}
obj.Response = responsecontent;
}
catch
{
obj.IsError = true;
}
}
}
catch (Exception ex)
{
if (ex.InnerException is TimeoutException)
{
ex = ex.InnerException;
}
obj.IsError = true;
obj.Exception = ex;
obj.Response = ex.Message;
}
return obj;
}
公共静态异步任务ProcessWebRequest(字符串url、T req、ILog记录器、int timeoutingseconds=0)
{
var obj=新的GenericPiResponse();
尝试
{
使用(var client=new HttpClient())
{
var jsonRequest=JsonSerializer.Serialize(req);
client.DefaultRequestHeaders.ExpectContinue=false;
var content=新的StringContent(jsonRequest);
content.Headers.Clear();
content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
如果(超时秒数>0)
{
client.Timeout=新的时间跨度(0,0,timeoutingseconds);
}
var response=wait client.PostAsync(url、内容);
obj.HttpResponseCode=response.StatusCode;
尝试
{
字符串responsecontent=null;
responsecontent=response.Content.ReadAsStringAsync().Result;
if(response.Content!=null&&response.Content.Headers!=null)
{
obj.ResponseContentType=response.Content.Headers.ContentType.MediaType;
if(responsecontent!=null&&obj.ResponseContentType==“text/html”)
{
if(responsecontent!=null&&responsecontent.Length>1000)
{
responsecontent=responsecontent.Substring(0,1000)+“…”;
}
}
}
对象响应=响应内容;
}
抓住
{
obj.IsError=true;
}
}
}
捕获(例外情况除外)
{
if(例如InnerException是TimeoutException)
{
ex=ex.InnerException;
}
obj.IsError=true;
对象异常=ex;
对象响应=例如消息;
}
返回obj;
}
但是得到了错误
System.Net.Http.HttpRequestException:将内容复制到流时出错。-->System.IO.IOException:响应过早结束
知道我的代码中缺少了什么或者我做错了什么吗
还没搞定。即使我已经超过90秒作为超时,但没有效果。
奇怪的是,有时它确实起作用了当该方法只被调用一次时,或者当您有多个调用时,会发生这种情况吗?。如果是第二个,则可能是客户端的可用套接字用完了。这就是为什么httpClient应该是一个singleton,它将相同的方法调用注入到其他一些api中,并且可以正常工作。看起来响应中有问题。请求的响应没有分块,对吗?因为您将其添加到请求头中,但您是否验证服务器是否遵守了此要求?请建议需要进行哪些更正在上述代码块中,如果您在客户端上两次调用Dispose(),using指令将在退出作用域时自动调用Dispose。尝试删除.Dispose()调用,看看会发生什么。您是否尝试过查看运行wireshark/fiddler代理时在线路上发生的情况?