Search code examples
c#urlsslhttps

Accessing HTTPS URL from Console Application using C#


I want my application to hit the HTTPS URL specified and download the CSV file from that URL.

I have the following code:

Program.cs

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; }
    }
}

AcceptAllCertifications.cs

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;
        }
    }
}

I am not receiving any error at compile time. But at run time, I am seeing the following error:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

How do I overcome this error?

Edit 1:

I tried to access the same URL from the browser and it is showing me the following screen:

enter image description here

Only after adding exception am I able to continue.

Edit 2:

After following answer's by @AndrewSilver and @Übercoder, I am seeing the following error:

The remote server returned an error: (411) Length Required

Thereafter I added httpWebRequest.ContentLength = 0;, which led me to the following error:

The remote server returned an error: (405) Method Not Allowed.

Thereafter I added httpWebRequest.ContentLength = 100;, which led me to the following error:

ProtocolViolationException: You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.

NOTE: Anyone who improves my answer by providing a solution without bypassing Certificate validation will be marked as accepted.


Solution

  • This code did the trick for me:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    
    namespace ReadCSVFromURL
    {
        class Program
        {
            static void Main(string[] args)
            {
                SplitCSV();
            }
    
            public static string GetCSV(string url)
            {
    
                ServicePointManager.ServerCertificateValidationCallback = 
                (object a, System.Security.Cryptography.X509Certificates.X509Certificate b, System.Security.Cryptography.X509Certificates.X509Chain c, System.Net.Security.SslPolicyErrors d) => { return true; };            
    
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string results = sr.ReadToEnd();
                sr.Close();
    
                return results;
            }
            public static void SplitCSV()
            {
                List<string> splitted = new List<string>();
                string fileList = GetCSV("URL");
                string[] tempStr;
                tempStr = fileList.Split(',');
                foreach (string item in tempStr)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        splitted.Add(item);
                    } 
                }
            }
        }
    }