.net MVC POST远程主机关闭了连接

wqsoz72f  于 2023-01-14  发布在  .NET
关注(0)|答案(2)|浏览(161)

我正在尝试跟踪错误并查看为什么会出现该错误。该页面是一个正在对API执行POST的表单。我看到的唯一错误是remote host closed the connection。从日志中我看到表单正在命中控制器并发送,但之后没有任何内容。是否有任何其他异常可以添加以帮助我理解此错误。
错误

System.Web.HttpException (0x80070057): The remote host closed the connection. The error code is 0x80070057.
   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
   at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async)
   at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
   at Telerik.Sitefinity.Modules.Libraries.Web.LibraryHttpHandler.

代码

protected ResultStatus FormCreate(requestWeb model)
    {
        var url = string.Format("Permission/SavePermissionRequestForm");

        var result = ApiHelpers.Post<ResultStatus>("POST", url, model);

        
        return result;
    }

 public static T Post<T>(string httpMethod, string url, object model)
        {
            try
            {
                var fullUrl = cmsApiUrl + url;

                var json = JsonConvert.SerializeObject(model);

                Stream dataStream = null;
                WebRequest Webrequest;
                Webrequest = WebRequest.Create(fullUrl);

                Webrequest.ContentType = "application/json";
                Webrequest.Method = WebRequestMethods.Http.Post;

                Webrequest.PreAuthenticate = true;
                Webrequest.Headers.Add("Authorization", "Bearer " + cmsApiKey);

                byte[] byteArray = Encoding.UTF8.GetBytes(json);

                Webrequest.ContentLength = byteArray.Length;

                dataStream = Webrequest.GetRequestStream();

                using (dataStream = Webrequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }

                WebResponse response = Webrequest.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                StringBuilder output = new StringBuilder();
                output.Append(reader.ReadToEnd());
                response.Close();

                T result = JsonConvert.DeserializeObject<T>(output.ToString());
                return result;
            }
            catch (Exception e)
            {
                T result = JsonConvert.DeserializeObject<T>("");
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
                return result;
            }
        }
ejk8hzay

ejk8hzay1#

1.这可能是服务器端的问题。如果您可以控制服务器端,请确保释放返回的StreamSource)。
1.另外,如果您可以控制远程服务器,请尝试启用缓冲。
1.异常是从第三方(Telerik.Sitefinity)抛出的。所以,正如其他人建议的那样-尝试从Postman或类似的工具中执行相同的请求,以更好地了解问题来自哪里。

hrysbysz

hrysbysz2#

我认为当您尝试再次读取流响应时会发生异常,而流响应之前已经被读取和使用过。您能指定此异常发生在代码的哪一行吗?这可能有助于我们给予您一个更准确的答案。谢谢

相关问题