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:
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.
This code did the trick for me:
Anyone who improves this code by providing a solution without bypassing Certificate validation will be marked as accepted.
It seems your certificate is not trusted. You can disable certificate validation or make certificate to be trusted (put certificate under trusted root manually, for example).
see SSL Certificate Issue - The remote certificate is invalid according to the validation procedure
Change this line:
to this:
And you can get rid of the other code you have. The problem with your code is that you were referencing the static
AcceptAllCertifications
, which you never set. So it always had a null, which in turn meant that you were not acutally assigning a value to it.