1

我刚刚从 ubuntu 软件中心安装了 monodevelop 并创建了一个解决方案。我正在尝试做 WebRequest.create(url)。但它一直抛出“身份验证或解密失败”。我究竟做错了什么?如果我没有做错任何事情,我应该怎么做才能完成上述工作!

我已经搜索过这个,但我无法想出一个可以解决我的问题的解决方案!

这就是我所做的......

public static string AccessURL(string url, string postcontent)
    {
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";
        byte[] byteArray = Encoding.UTF8.GetBytes(postcontent);
        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 = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();
        return responseFromServer;
    }

提前致谢!

4

1 回答 1

4

该错误表明您正在尝试访问 SSL/TLS 加密资源。Mono 默认情况下不信任任何这些站点,由开发人员决定是否要访问该资源。在mono 的安全常见问题解答中了解更多信息。

因此,一种方法是在您的类中实现 ServerCertificateValidationCallback:

using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

public class MyAwesomeProgram
{
    public MyAwesomeProgram() 
    {
        ServicePointManager.ServerCertificateValidationCallback =
                ValidateServerCertficate;
    }

    private static bool ValidateServerCertficate(object sender, X509Certificate certificate,
        X509Chain chain, SslPolicyErrors sslpolicyerrors)
    {
        //This is where you should validate the remote certificate
        return true;
    }

    public void FetchAwesomeStuff (string url) 
    {
        var wr = WebRequest.Create (url);
        var stream = wr.GetResponse().GetResponseStream ();
        Console.WriteLine (new StreamReader (stream).ReadToEnd ());
    }
}

mono 文档中有更多关于使用 Certificate Policies 和 Trusting Roots的示例。请注意,上面的示例忽略了安全性并接受所有证书,即使是您可能不信任的证书。这个不推荐使用!

于 2012-09-21T09:02:59.880 回答