.NET 4.6.2"值不能为空"WebRequest.GetResponse()异常

Dam*_*les 2 .net c# webrequest

当我在.NET 4.6.2上运行以下代码时,

var req = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "key=my real key is here");

string json = "{\r\n  \"to\": \"/topics/news\",\r\n  \"data\": {\r\n    \"item0\": \"0\",\r\n    \"item1\": \"1\"\r\n  }\r\n}";
var jsonbytes = Encoding.UTF8.GetBytes(json);

var reqStr = req.GetRequestStream();
reqStr.Write(jsonbytes, 0, jsonbytes.Length);
reqStr.Close();

var res = (HttpWebResponse)req.GetResponse();
Debug.WriteLine(res.StatusCode);
var sr = new StreamReader(res.GetResponseStream());
Debug.Write(sr.ReadToEnd());
sr.Close();
Run Code Online (Sandbox Code Playgroud)

以下异常发生在var res = (HttpWebResponse)req.GetResponse();.

Exception   Exception thrown: 'System.ArgumentNullException' in mscorlib.dll ("Value cannot be null.")  System.ArgumentNullException
Run Code Online (Sandbox Code Playgroud)

我无法获得更多信息.我不知道什么值是null.我在.NET 4.5.2上运行了相同的代码,没有发生异常.为什么会发生这种异常?

Ben*_*itz 6

这是因为4.6.2中添加了一些代码,它使用try/catch来解析注册表中字符串的枚举,这显然是大多数人没有的.

关于Connect的一个问题已经开启了,但是像往常一样,它将无处可去,并且将在5年内无法解决.

我不明白他们为什么不使用TryParse.而且我不明白他们为什么会这么多基于异常的代码,而Visual Studio中的异常支持很少.现在我每次启动项目时都会失去30秒因为我在初始化开始时中断 - 禁用ArgumentNullException对我来说不是一个选项,如果它在我的应用程序代码中发生,我想立即中断.

我认为在Visual Studio 2017中,您最终将能够禁用每个程序集的异常,因此如果这很麻烦,您可以随意下载预览.