只问耕耘
分类: WINDOWS
2011-07-08 09:55:39
PRB: "System.Net.WebException. The Underlying Connection Was Closed. Could Not Establish Trust Relationship with Remote Server." Error Message When You Upgrade the .NET Framework
To work around this problem, you can implement ICertificatePolicy. Then you must pass ICertificatePolicy to ServicePointManager.CertificatePolicy before the Web Service method call is made.
The following code sample is implemented in the client application. The code forces the client application to accept every certificate that the server provides. This method weakens the security of the application because the authentication of the server is bypassed. To safely work around this problem, make sure that the certificate of the server contains the expected name before you allow the connection to continue.
The following sample code implements ICertificatePolicy and then accepts every request under SSL:
Microsoft Visual Basic .NET
Import the following two namespaces, and then implement the class:
Imports System.Net
Imports System.Security.Cryptography.X509Certificates
Public Class MyPolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As ServicePoint, _
ByVal cert As X509Certificate, ByVal request As WebRequest, _
ByVal certificateProblem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
'Return True to force the certificate to be accepted.
Return True
End Function
End Class
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class MyPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint
, X509Certificate certificate
, WebRequest request
, int certificateProblem) {
//Return True to force the certificate to be accepted.
return true;
} // end CheckValidationResult
} // class MyPolicy
System.Net.ServicePointManager.CertificatePolicy = New MyPolicy()
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();