Jefferson
Jefferson

Reputation: 57

MVC POST The remote host closed the connection

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;
            }
        }

Upvotes: 1

Views: 762

Answers (2)

zaarour
zaarour

Reputation: 105

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

Upvotes: 1

itsho
itsho

Reputation: 4800

  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.

Upvotes: 1

Related Questions