gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:38:17 25 4
gpt4 key购买 nike

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

我有以下代码:

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

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

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

如何克服这个错误?

编辑1:

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

enter image description here

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

编辑2:

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

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

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

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

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

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

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

最佳答案

这段代码对我有用:

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

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

关于c# - 使用 C# 从控制台应用程序访问 HTTPS URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com