I have been doing some test migrating .NET Framework 4.6.2 apps to .NET Core 2. I noticed that this particular app, a monitoring http is not working fine with Net Core 2. Can you please help me to verify what is happening?
static void Main(string[] args)
{
try
{
HttpWebRequest myhttpWebReqest = (HttpWebRequest)WebRequest.Create("https://www.google.com.mx/");
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myhttpWebReqest.GetResponse();
timer.Stop();
TimeSpan timeSpan = timer.Elapsed;
Console.WriteLine(timeSpan.ToString());
Console.WriteLine();
Console.WriteLine(myHttpWebResponse.StatusCode);
Console.WriteLine((int)myHttpWebResponse.StatusCode);
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetEffectiveDateString());
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetExpirationDateString());
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Issuer);
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Subject);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if(ex.InnerException !=null)
{
Console.WriteLine(ex.InnerException);
}
}
Console.ReadLine();
}
}
in the .NET Framework 4.6.2 i see the certificate data, in the .NET Core 2 i see myhttpWebReqest.ServicePoint.Certificate null ... do you know why?
See discussion of this here: https://github.com/dotnet/corefx/issues/36979
ServicePointManager and ServicePoint classes are no-op on .NET Core. But you can do a similar thing with HttpClient. HttpClient is the more modern and preferred HTTP API in .NET Framework and .NET Core.
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace NetCoreConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = CustomCallback;
var client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync("https://www.google.com.mx/").GetAwaiter().GetResult();
Console.WriteLine(response.StatusCode);
Console.WriteLine((int)response.StatusCode);
}
private static bool CustomCallback(HttpRequestMessage arg1, X509Certificate2 arg2, X509Chain arg3, SslPolicyErrors arg4)
{
Console.WriteLine(arg2.GetEffectiveDateString());
Console.WriteLine(arg2.GetExpirationDateString());
Console.WriteLine(arg2.Issuer);
Console.WriteLine(arg2.Subject);
return arg4 == SslPolicyErrors.None;
}
}
}