gpt4 book ai didi

c# - 在使用 monodevelop 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:24 25 4
gpt4 key购买 nike

我刚刚从 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;
}

提前致谢!

最佳答案

该错误表明您正在尝试访问 SSL/TLS 加密资源。默认情况下,Mono 不信任任何这些站点,由开发人员决定您是否要访问该资源。更多信息请参见 mono's security FAQ .

因此,一种方法是在您的类中实现 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 documentation about using Certificate Policies and Trusting Roots 中有更多示例. 注意上面的示例忽略了安全性并接受所有证书,即使是您可能不信任的证书。不建议使用!

关于c# - 在使用 monodevelop 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12527243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com