I'm trying to check our SSL certificates with a program but i have an issue; i can not use second HttpWebRequest because of something which i don't know. I supposed it's a cache problem and used all of the no-cache codes that i know. As you can see servers are in our private subnet and 1 server has a lot of IIS application. Because of that i'm changing "request.Host" variable to our matching IIS bindings for open the sites. Always second one is not working but first one works well. What is the problem?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
request.AllowAutoRedirect = false;
request.Host = "www.xxx.com";
request.Timeout = 30 * 1000;
request.Headers.Add("Cache-Control", "no-cache");
request.Referer = null;
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;
//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert));
}
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
request2.AllowAutoRedirect = false;
request2.Host = "www.yyy.com";
request2.Timeout = 30 * 1000;
request2.Headers.Add("Cache-Control", "no-cache");
request2.Referer = null;
request2.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
{
//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert2 = request2.ServicePoint.Certificate;
//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert2));
}
Error:
The remote server returned an error: (400) Bad Request.
Exception Line:
using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
Status: ProtocolError
InternalStatus: RequestFatal
I solved my problem. The problem was keep-alive attribute and because of that second SSL connection didn't initialized properly. You have to add this line to your requests.
request.KeepAlive = false;