我希望我的应用程序点击指定的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;
}
}
}
我在编译时没有收到任何错误。但在运行时,我看到以下错误:
底层连接已关闭:无法为SSL / TLS安全通道建立信任关系
如何克服此错误?
修改1:
我尝试从浏览器访问相同的网址,它显示以下屏幕:
只有在添加异常后才能继续。
编辑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来执行此操作。
注意:任何通过提供解决方案而不绕过证书验证来改善我的答案的人都将被标记为已接受。
答案 0 :(得分:4)
这段代码对我有用:
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);
}
}
}
}
}
通过提供解决方案而不绕过证书验证来改进此代码的任何人都将被标记为已接受。
答案 1 :(得分:0)
更改此行:
ServicePointManager.ServerCertificateValidationCallback =
new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
到此:
ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => { return true;};
你可以摆脱你拥有的其他代码。您的代码的问题在于您引用了您从未设置的静态AcceptAllCertifications
。所以它总是有一个null,这反过来意味着你没有实际为它分配一个值。
答案 2 :(得分:0)
您的证书似乎不受信任。您可以禁用证书验证或使证书可信(例如,手动将证书置于受信任的根目录下)。
请参阅SSL Certificate Issue - The remote certificate is invalid according to the validation procedure