I have been using the following code (vb.Net-3.5) in the PayPal Sandbox for 2 months with no problems. Now it throws the error: The underlying connection was closed: An unexpected error occurred on a send
Any ideas on what should be done to make it work again.
Could this have anything to do with the changes PayPal is implementing on the SSL Certificates in Sandbox. This started happening after 1/18/2016. I found this article about the PayPal SSL Certificate update https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1766
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Collections.Generic
Imports System.Object
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Imports System.Exception
Imports System.SystemException
Imports System.Threading.ThreadAbortException
Partial Class Payment
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' This is the initial start of program
Dim QString As String = ""
Dim parcelID As String = ""
Dim txID As String = ""
' Get Query string Variables
QString = Request.ServerVariables("QUERY_STRING")
parcelID = Request.QueryString("item_number")
txID = Request.QueryString("tx")
Try
If Not String.IsNullOrEmpty(txID) Then
' Create a request using a URL that can receive a post.
' Sandbox Version
Dim PayPalWeb As String = "https://www.sandbox.paypal.com/cgi-bin/webscr"
Dim PayPalID As String = "***********************************************************"
' Live Version
'Dim PayPalWeb As String = "https://www.paypal.com/cgi-bin/webscr"
'Dim PayPalID As String = ""
Dim PayPalRequest As WebRequest
' Create POST data and convert it to a byte array.
Dim postData As String = "cmd=_notify-synch" + _
"&tx=" + txID + _
"&at=" + PayPalID + _
"&submit = PDT"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Dim dataStream As Stream
PayPalRequest = WebRequest.Create(PayPalWeb)
' Set the Method property of the request to POST.
PayPalRequest.Method = "POST"
' Set the ContentType property of the WebRequest.
PayPalRequest.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
PayPalRequest.ContentLength = byteArray.Length
' Get the request stream.
dataStream = PayPalRequest.GetRequestStream() ' <===== Fails on this call
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim PayPalResponse As WebResponse = PayPalRequest.GetResponse()
' Get the stream containing content returned by the server.
dataStream = PayPalResponse.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Clean up the streams.
reader.Close()
dataStream.Close()
PayPalResponse.Close()
.......
My problem was a result of the update PayPal is making to their security.
See https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1766
In my case, I had to upgrade to .NEt Framework 4.5 and include the following:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
before the call dataStream = PayPalRequest.GetRequestStream()
Try this instead:
You're passing name/value pairs. A PayPalesque explanation follows.
https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1348&viewlocale=en_US&direct=en