这是Visual Studio 2015或?
的错误Exception Exception thrown: 'System.ArgumentNullException' in
mscorlib.dll ("Value cannot be null.") System.ArgumentNullException
我正在关注的代码/教程...
https://www.codeproject.com/tips/397574/use-csharp-to-get-get-json-data-data-from-the-web-and-map-i
复制创建一个表单应用程序,并添加一个称为" button1"的按钮,对于button1_click函数添加此代码,URL中的http没有错误,https给出了错误...
var w = new WebClient();
string url = "https://www.msftncsi.com/ncsi.txt";
var content = w.DownloadString(url);
MessageBox.Show(content);
如果您只使用必须调用https
的代码,则它将行不通。尝试导入这些名称空间:
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
然后进行证书验证:
System.Net.ServicePointManager.ServerCertificateValidationCallback =
delegate (object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
那么它应该有效:
var w = new WebClient();
string url = "https://www.msftncsi.com/ncsi.txt";
var content = w.DownloadString(url);
请阅读此内容,以了解为什么上述方法可能很危险。