-1

Is this is a bug in Visual Studio 2015 or?

Exception Exception thrown: 'System.ArgumentNullException' in 
      mscorlib.dll ("Value cannot be null.") System.ArgumentNullException

The code/tutorial I was following...

https://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-Data-from-the-Web-and-Map-i

To reproduced create a form application and add a button called "button1" and for button1_Click function add this code, http in the URL gives no error, https gives an error...

var w = new WebClient();
string url = "https://www.msftncsi.com/ncsi.txt";
var content = w.DownloadString(url);
MessageBox.Show(content);
Prescott
  • 7,312
  • 5
  • 49
  • 70
user576820
  • 1,289
  • 2
  • 11
  • 13
  • In what way is this uncatchable? What specifically have you done to try to catch it? –  Feb 19 '17 at 01:50
  • I tried putting it in a try{ ... } catch(Exception e){ MessageBox.Show("error"); throw e; } and got no response. – user576820 Feb 19 '17 at 11:01
  • And what happens in your program? Does the whole program crash? Does it continue? Does it show an unhandled exception dialog box? Also, is the exception's stack trace visible anywhere, so that you can see specifically what is throwing the exception? –  Feb 19 '17 at 11:18

1 Answers1

0

If you just use the code you have to call https, it will not work. Try importing these namespaces:

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

Then do this for certificate verification:

System.Net.ServicePointManager.ServerCertificateValidationCallback =
    delegate (object sender, X509Certificate certificate, 
    X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { return true; };

Then it should work:

var w = new WebClient();
string url = "https://www.msftncsi.com/ncsi.txt";
var content = w.DownloadString(url);

Please read this also to be aware of why the above approach could be dangerous.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64