locked
WebRequest : The underlying connection was closed: An unexpected error occurred on a send RRS feed

  • Question

  • Wondering if anyone could provide some insight to this.  System written in ASP.Net VB hosted by Azure.

    I have a few vendors who I submit XML files via WebRequest.  The system has worked fine both in production (Azure) and on my development station until today.

    I am now receiving an error, "The underlying connection was closed: An unexpected error occurred on a send.'   This error occurs when getting the request stream "request.GetRequestStream()" and occurs on the Azure website, and my development station.  The only thing I can think of is that there was some security update that Microsoft did that affected my local station and Azure.

    Snippet of code is as follows:

      ' Create a request using a URL that can receive a post. 
                Dim request As WebRequest ="https://dev.mms.TheWebSite.com/services/b2b/BZadZo8McaHXco4aTvrR0C1WkRMN87/cxml")
    
                ' Set the Method property of the request to POST.
                request.Method = "POST"
    
                ' Create POST data and convert it to a byte array.
                Dim postData As String =(Get the data here)
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    
                ' Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length
    
                ' Get the request stream.
    
    'CRASHES WITH ERROR HERE:
                Dim dataStream As Stream = request.GetRequestStream()
    
    
    
    
    
    
    Any help would be appreciated!
     
    Thursday, February 15, 2018 12:17 AM

Answers

  • After a bit of research, an adjustment to the Security Protocol fixed the issue, thanks to this link:

    https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/

    Added:

    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

    to accommodate TLS1.2 in .Net Framework 4.0

    Thursday, February 15, 2018 2:37 AM
  • After a bit of research, an adjustment to the Security Protocol fixed the issue, thanks to this link:

    https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/

    Added:

    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

    to accommodate TLS1.2 in .Net Framework 4.0

     

    Thursday, February 15, 2018 2:36 AM

All replies

  • Wondering if anyone could provide some insight to this.  System written in ASP.Net VB hosted by Azure.

    (not sure if this is the appropriate forum for this question)

    I have a few vendors who I submit XML files via WebRequest.  The system has worked fine both in production (Azure) and on my development station until today.

    I am now receiving an error, "The underlying connection was closed: An unexpected error occurred on a send.'   This error occurs when getting the request stream "request.GetRequestStream()" and occurs on the Azure website, and my development station.  The only thing I can think of is that there was some security update that Microsoft did that affected my local station and Azure.

    Snippet of code is as follows:

      ' Create a request using a URL that can receive a post. 
                Dim request As WebRequest ="https://dev.mms.TheWebSite.com/services/b2b/BZadZo8McaHXco4aTvrR0C1WkRMN87/cxml")
    
                ' Set the Method property of the request to POST.
                request.Method = "POST"
    
                ' Create POST data and convert it to a byte array.
                Dim postData As String =(Get the data here)
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    
                ' Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length
    
                ' Get the request stream.
    
    'CRASHES WITH ERROR HERE:
                Dim dataStream As Stream = request.GetRequestStream()
    
    
    
    
    
    
    Any help would be appreciated!
     
    • Merged by Sheethal J S Thursday, February 15, 2018 3:25 AM similar Query
    Thursday, February 15, 2018 12:23 AM
  •  "The underlying connection was closed: An unexpected error occurred on a send.'   

    All you're getting is the generic exception being thrown, and you don't know the real reason the underlying exception message my give as to the why.

    You can only get it from the inner.exception.message if there, which is part of the stack trace. 

    You should put a try/catch around the code and get the stack trace with the inner.exception.message if there.

    Thursday, February 15, 2018 12:54 AM
  • After a bit of research, an adjustment to the Security Protocol fixed the issue, thanks to this link:

    https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/

    Added:

    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

    to accommodate TLS1.2 in .Net Framework 4.0

     

    Thursday, February 15, 2018 2:36 AM
  • After a bit of research, an adjustment to the Security Protocol fixed the issue, thanks to this link:

    https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/

    Added:

    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

    to accommodate TLS1.2 in .Net Framework 4.0

    Thursday, February 15, 2018 2:37 AM
  • Been looking for a fix for days, literally hours and can say this solution actually worked for me, thank you.
    Monday, August 13, 2018 8:38 PM
  • Hi,

    We are experiencing the same issue.

    We are using GetRequestStream()to connect to Dundas BI from our Web Application.

    But the below exception throws up. 

    WebException: The underlying connection was closed: An unexpected error occurred on a send.

    We already have the below line to control TLS and our code is in .NET 4.6.1

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    Any help is appreciated.

    Wednesday, March 13, 2019 11:07 PM