user7898131
user7898131

Reputation:

Paypal the underlying connection was closed

I am trying to get the response from paypal sandbox environment to test the variables coming back from paypal. I am using asp.net and vb.net now the code works fine on the live paypal url but not on the local one for some strange reason.

I want to use the sandbox environment for local testing but I am presented with the following error.

An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code

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

Now the code works live on

 Dim req As HttpWebRequest = CType(WebRequest.Create("https://www.paypal.com/cgi-bin/webscr"), HttpWebRequest)

I had assumed that all I change was this

  Dim req As HttpWebRequest = CType(WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), 

Main Code

 Dim req As HttpWebRequest = CType(WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr"), HttpWebRequest)
    'Set values for the request back
    req.Method = "POST"
    req.ContentType = "application/x-www-form-urlencoded"
    Dim Param() As Byte = Request.BinaryRead(HttpContext.Current.Request.ContentLength)
    Dim strRequest As String = Encoding.ASCII.GetString(Param)
    strRequest = strRequest + "&cmd=_notify-validate"
    req.ContentLength = strRequest.Length

    'Send the request to PayPal and get the response
    Dim streamOut As StreamWriter = New StreamWriter(req.GetRequestStream(), Encoding.ASCII)
    streamOut.Write(strRequest)
    streamOut.Close()
    Dim streamIn As StreamReader = New StreamReader(req.GetResponse().GetResponseStream())
    Dim strResponse As String = streamIn.ReadToEnd()
    streamIn.Close()

    'Assign payment variables 
    strOrderNo = HttpContext.Current.Request("item_number")
    strGross = HttpContext.Current.Request("mc_gross")

Edit 2 After further investigations it appears to be the way paypal changed their sandbox but I cannot upgrade my application to 4.5 to just use sandbox is their any option open to me?. As per this stack

Using PayPal PDT with vb.net and getting: the underlying connection was closed: An unexpected error occurred on a send

Which suggests to change this line to use Tsl12 but that is not an option in my web site and I cannot upgrade it as its a daily used site at moment.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

Why is it the code work fine in the live environment why has paypal changed their sandbox to do this ?

Upvotes: 1

Views: 684

Answers (1)

user7898131
user7898131

Reputation:

For anyone else struggling I found how to fix this using the following hack

Const _Tls12 As SslProtocols = DirectCast(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = DirectCast(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12

Thanks to this great op at How to implement Security Protocols TLS 1.2 in .Net 3.5 framework

Can anyone let me no the implications of using the above hack will it be future proof.

Upvotes: 1

Related Questions