Accessing HTTPS URL from Console Application using

2019-02-15 12:46发布

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.

标签: c# url ssl https
3条回答
Lonely孤独者°
2楼-- · 2019-02-15 13:22

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

Anyone who improves this code by providing a solution without bypassing Certificate validation will be marked as accepted.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-15 13:34

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

查看更多
叛逆
4楼-- · 2019-02-15 13:43

Change this line:

ServicePointManager.ServerCertificateValidationCallback =
     new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

to this:

ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => { return true;};

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.

查看更多
登录 后发表回答