Problem: The underlying connection was closed: Could not establish trust relationship with remote server.
Solution: You can solve this in your code by creating your own
CertificatePolicy class (which implements the
ICertificatePolicy interface).
In this class you will have to write your own
CheckValidationResult function that has to return true or false, like you would press yes or no in the dialog window.
Please note: For development purposes I've created the following class which accepts all certificates, so you won't get the nasty
WebException anymore:
public class Payment
{
internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
}
public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert,
WebRequest wRequest, int certProb)
{
return true;
}
}
public static string Pay()
{
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
HttpWebRequest request = null;
request = (HttpWebRequest)WebRequest.Create("https://api-3t.sandbox.paypal.com/nvp");
request.Method = "POST";
string formContent = "Your Query string goes here"; // Change the post variables with values here
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = Common.UrlDecode(reader.ReadToEnd());
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
As you can see the
CheckValidationResult function always returns true, so all certificates will be trusted. If you want to make this class a little bit more secure, you can add additional checks using the X509Certificate parameter for example. To use this CertificatePolicy, you'll have to tell the
ServicePointManager to use it:
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
This must be done (one time during the application life cycle) before making the call to your HttpWebRequest or a Webservice.