【发布时间】:2016-02-10 07:12:44
【问题描述】:
我希望我的应用程序访问指定的 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:
我尝试从浏览器访问相同的 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 来执行此操作。
注意:任何通过不绕过证书验证提供解决方案来改进我的答案的人都将被标记为接受。
【问题讨论】:
-
您是否尝试过从浏览器获取 url 中的文件?它很可能会失败,因为您从中请求文件的服务器没有 SSL 所需的正确证书。
-
@Übercoder 检查我更新的问题
-
这和SFTP无关,你的问题是关于HTTPS的。
-
@MartinPrikryl 好的。但是我也能用这段代码访问 SFTP URL 吗?还是我必须使用第三方解决方案进行重大更改?
-
更准确地说,我可以使用
HttpWebRequest访问SFTP URL 吗?