Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1535

Accessing HTTPS URL from Console Application using C#

$
0
0

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.


Viewing all articles
Browse latest Browse all 1535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>