1

I am trying to track down the error and see why I am getting it. The page is a form that is doing a POST to a API. The only error I see is that the remote host closed the connection. From the logs I see the Form is hitting the controller and sending but nothing after that. Is there a any other Exception I can add to help me understand this error.

Error

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.

Code

    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;
            }
        }
Jefferson
  • 173
  • 2
  • 12
  • 32
  • What version of .net? (https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated). Why is this question tagged asp.net-mvc if your code is all about a web client, not a web server? – Jeremy Lakeman Dec 21 '22 at 04:42
  • Version is .net 4.8 I have removed the asp tagged – Jefferson Dec 21 '22 at 18:21
  • 1
    check the value of fullUrl is it is correct, if there is any ssl issue. try to post the same request from postman – Mukul Keshari Jan 05 '23 at 14:41
  • My guess is that the server you are requesting is using HttpResponse.Close() method "This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing." (source: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.close?view=netframework-4.8) If you have access to the server side try to change that behavior – Max Jan 12 '23 at 08:16

2 Answers2

1
  1. This could be a server side issue. if you have control over the server side, be sure to dispose the Stream you return (Source).
  2. also, If you have control on the Remote Server, try to enable buffering.
  3. The exception is thrown from a 3rd party (Telerik.Sitefinity). so, as others suggested - try to do the same request from Postman or similar tool to better understand where's the issue is coming from.
itsho
  • 4,640
  • 3
  • 46
  • 68
1

I think the exception happens when you try to read the stream response again when it has already been read and consumed before. Can you please specify which line of your code this exception occurs in? This could help us to give you a more precise answer. Thanks

zaarour
  • 107
  • 1
  • 4