How to ignore a certificate error with c# 2.0 WebClient - without the certificate

后端 未结 6 2121
时光说笑
时光说笑 2020-11-28 06:42

Using Visual Studio 2005 - C# 2.0, System.Net.WebClient.UploadData(Uri address, byte[] data) Windows Server 2003

So here\'s a stripped down version of

6条回答
  •  不知归路
    2020-11-28 07:02

    This is somewhat the code we're using (not polished yet - I don't think I have the error-handling setup correctly but it should be close) based on thomas's suggestion (this is .NET 4.0 code, though):

    var sslFailureCallback = new RemoteCertificateValidationCallback(delegate { return true; });
    
    try
    {
    
        if (ignoreSslErrors)
        {
            ServicePointManager.ServerCertificateValidationCallback += sslFailureCallback;
        }
    
        response = webClient.UploadData(Options.Address, "POST", Encoding.ASCII.GetBytes(Options.PostData));
    
    }
    catch (Exception err)
    {
        PageSource = "POST Failed:\r\n\r\n" + err;
        return PageSource;
    }
    finally
    {
        if (ignoreSslErrors)
        {
            ServicePointManager.ServerCertificateValidationCallback -= sslFailureCallback;
        }
    }
    

提交回复
热议问题