title: Accessing HTTPS URL from Console Application using C#
body: <p>I want my application to hit the HTTPS URL specified and download the CSV file from that URL.</p>
  
  <p>I have the following code:</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Net.Security;
  using System.IO;
  
  namespace httpWebRequest_Test
  {
      class Program
      {
          static void Main(string[] args)
          {
              var webAddr = "https://SFTP URL/xyz.csv";
              var httpWebRequest = (HttpWebRequest) WebRequest.Create(webAddr);
              httpWebRequest.ContentType = "text/csv";
              httpWebRequest.Method = "POST";
              var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
              //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
              ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
              Stream resStream = httpResponse.GetResponseStream();
          }
  
          AcceptAllCertification aac = new AcceptAllCertification();
  
          public static RemoteCertificateValidationCallback AcceptAllCertifications { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>AcceptAllCertifications.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace httpWebRequest_Test
  {
      class AcceptAllCertification
      {
          public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
          {
              return true;
          }
      }
  }
  </code></pre>
  
  <p>I am not receiving any error at compile time. But at run time, I am seeing the following error:</p>
  
  <blockquote>
    <p>The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel</p>
  </blockquote>
  
  <p>How do I overcome this error?</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I tried to access the same URL from the browser and it is showing me the following screen:</p>
  
  <p><a href="https://i.stack.imgur.com/7r8i7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7r8i7.png" alt="enter image description here"></a></p>
  
  <p>Only after adding exception am I able to continue.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>After following answer's by @AndrewSilver and @Übercoder, I am seeing the following error:</p>
  
  <blockquote>
    <p>The remote server returned an error: (411) Length Required</p>
  </blockquote>
  
  <p>Thereafter I added <code>httpWebRequest.ContentLength = 0;</code>, which led me to the following error:</p>
  
  <blockquote>
    <p>The remote server returned an error: (405) Method Not Allowed.</p>
  </blockquote>
  
  <p>Thereafter I added <code>httpWebRequest.ContentLength = 100;</code>, which led me to the following error:</p>
  
  <blockquote>
    <p>ProtocolViolationException: You must provide a request body if you set ContentLength>0 or SendChunked==true.  Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.</p>
  </blockquote>
  
  <p><strong>NOTE:</strong> Anyone who improves my answer by providing a solution <strong>without bypassing Certificate validation</strong> will be marked as accepted.</p>