使用 C# 从控制台应用程序访问 HTTPS URL

Yas*_*iya 2 c# url ssl https

我希望我的应用程序访问指定的 HTTPS URL 并从该 URL 下载 CSV 文件。

我有以下代码:

程序.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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在编译时没有收到任何错误。但是在运行时,我看到以下错误:

底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系

我如何克服这个错误?

编辑1:

我试图从浏览器访问相同的 URL,它显示了以下屏幕:

在此处输入图片说明

只有在添加异常后,我才能继续。

编辑2:

按照@AndrewSilver 和@Übercoder 的回答之后,我看到以下错误:

远程服务器返回错误:(411) Length Required

此后我添加了httpWebRequest.ContentLength = 0;,这导致我出现以下错误:

远程服务器返回错误:(405) Method Not Allowed。

此后我添加了httpWebRequest.ContentLength = 100;,这导致我出现以下错误:

ProtocolViolationException:如果设置 ContentLength>0 或 SendChunked==true,则必须提供请求正文。通过在 [Begin]GetResponse 之前调用 [Begin]GetRequestStream 来完成此操作。

注意:通过提供解决方案而不绕过证书验证来改进我的答案的任何人都将被标记为已接受。

Yas*_*iya 5

这段代码对我有用:

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);
                } 
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过提供解决方案而不绕过证书验证来改进此代码的任何人都将被标记为已接受。