title: C# Download all https certificates from a website
body: <p>I want to save all certificates from a URL to disk. So for example <em><a href="https://www.google.de" rel="nofollow noreferrer">https://www.google.de</a></em> </p>
  
  <p>If I browse this page with Firefox, I can see three certificates.
  <a href="https://i.stack.imgur.com/acO0o.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/acO0o.jpg" alt="enter image description here"></a></p>
  
  <p>With Firefox I can export them all, and save them to disk.</p>
  
  <p>So I want to do that in C#.
  I started getting the certificates with the following code..</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Get and write certificate from URL into file in path
      /// &lt;/summary&gt;
      /// &lt;param name="_URL"&gt;URL of website with certficate&lt;/param&gt;
      /// &lt;param name="_path"&gt;Path where you want to store certificate&lt;/param&gt;
      private static void SaveCertificate(String _URL, String _path)
      {
          try
          {
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_URL);
              request.AllowAutoRedirect = false;
  
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              response.Close();
  
              X509Certificate2 cert = new X509Certificate2(request.ServicePoint.Certificate);
  
              File.WriteAllText(_path, ExportToPEM(cert));
          }
          catch (Exception)
          {
          }
      }
  
      /// &lt;summary&gt;
      /// Export a certificate to a PEM format string
      /// &lt;/summary&gt;
      /// &lt;param name="_cert"&gt;The certificate to export&lt;/param&gt;
      /// &lt;returns&gt;A PEM encoded string&lt;/returns&gt;
      public static string ExportToPEM(X509Certificate2 _cert)
      {
          StringBuilder builder = new StringBuilder();
  
          try
          {
              builder.AppendLine("-----BEGIN CERTIFICATE-----");
              builder.AppendLine(Convert.ToBase64String(_cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
              builder.AppendLine("-----END CERTIFICATE-----");
  
          }
          catch (Exception)
          {
          }
  
          return builder.ToString();
      }
  </code></pre>
  
  <p>The problem with this code is, I just get one certificate exept three. (the certificate I get is the marked certificate in the screenshot from firefox)</p>
  
  <p>I also tryed the solution from here <a href="https://stackoverflow.com/questions/24618798/automated-downloading-of-x509-certificate-chain-from-remote-host">SO Question</a> but it didnt work for me. </p>
  
  <p>My question is, how can I get all certificates from a URL? </p>