The connection was closed unexpectedly for System.Net.HttpWebRequest.GetRequestStream() [Answered]RSS

3 replies

Last post Mar 16, 2020 07:14 AM by J1509

  • The connection was closed unexpectedly for System.Net.HttpWebRequest.GetRequestStream()

    Mar 12, 2020 01:47 PM|J1509|LINK

    I created a web Asp.Net application (web forms) in c# with Sql Server Express for an online store integrated with PayPal. I used the code for pdt given at the following link https://github.com/paypal/pdt-code-samples/blob/master/paypal_pdt.cs. Everything worked perfectly during development using sendbox and Visual Studio 2019, but on Windows Server 2016, after the payment on PayPal by redirecting to the page on the site in a protected area, the connection drops and generates an error on the line of code StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII). I can't understand what is wrong. 

    I added the following code:

    • on the redirect page --> System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    • on the web.config --> binding openTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00" />

    Also I checked the SSL certificate, that is ok. I update the framework to the latest version.

    On IIS 8 there's this error:

    Event code: 3005 Event message: An unhandled exception has occurred. Event sequence: 22 Event occurrence: 1  Event detail code: 0  Application information:  Trust level: Full  Process information:  Process ID: 6472  Process name: w3wp.exe  Account name: NT AUTHORITY\SYSTEM  Exception information:  Exception type: WebException Exception message: The underlying connection was closed: The connection was closed unexpectedly.    at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)  at System.Net.HttpWebRequest.GetRequestStream()    at website.ProtectArea.shopping.Page_Load(Object sender, EventArgs e) in C:\xxxx\website\shopping.aspx.cs:line 44    at System.Web.UI.Control.OnLoad(EventArgs e)    at System.Web.UI.Control.LoadRecursive()    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Request information:     Request URL: https://website.com/ProtectArea/shopping + token info from PayPal     Request path: /ProtectArea//shopping     User host address: xx.xxxx.xxx.xx     User: xxxx@xxxxx.com  Is authenticated: True  Authentication Type: ApplicationCookie Thread account name: NT AUTHORITY\SYSTEM Thread information:  Thread ID: 16 Thread account name: NT AUTHORITY\SYSTEM Is impersonating: False

     

  • Re: The connection was closed unexpectedly for System.Net.HttpWebRequest.GetRequestStream()

    Mar 13, 2020 02:58 AM|Jalpa Panchal|LINK

    Hi,

    Are you using TLS 1.2? You could try to one of the below way to resolve the issue:


    1. Upgrade to .NET 4.6 or newer. TLS 1.2 should then be enabled by default.

    2. Make sure that you have a version of .NET that supports TLS 1.2(https://docs.microsoft.com/en-us/configmgr/core/plan-design/security/enable-tls-1-2#update-net-framework-to-support-tls-12)

    You could refer this below link for more detail:

    https://stackoverflow.com/questions/48517132/how-to-resolve-http-web-request-underlying-connection-was-closed-error-calling-a

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: The connection was closed unexpectedly for System.Net.HttpWebRequest.GetRequestStream()

    Mar 14, 2020 09:18 AM|J1509|LINK

    Hi Jalpa, thanks for replying. 

    I updated .Net Framework to the version 4.7 and TLS 1.2 is configured correctly using Qualsys SSL Labs (the site has an A for overall rating). I also checked everything on the Registry Editor.
    I am sure that it is not the page code that blocks the connection, because it is blocked as soon as the redirection (exact) from PayPal to the site page... but I have no idea what it is!
    Do you have any idea?

  • Re: The connection was closed unexpectedly for System.Net.HttpWebRequest.GetRequestStream()

    Mar 16, 2020 07:14 AM|J1509|LINK

    Thank you, I solved the problem.