我创建了一个SSIS包,使用C#脚本从HTTPS URL下载CSV文件。从Visual Studio执行时,一切正常。
但是,当我创建SQL代理作业时,包失败。如果我直接从SQL Server执行.dtsx文件,该包也会失败。
错误是
"下载失败:从服务器获取的SSL证书响应 无效。无法处理请求"
我已经测试过从SQL服务器打开URL并可以查看该文件。 我还在C#脚本中添加了代码以忽略认证错误,但这似乎没有任何影响。 我还在Connection Manager中输入了托管CSV的服务器的登录凭据,但也没有帮助。
CSV是通过Magento数据Feed生成的。在网站上,所有内容都被重定向到HTTPS。
我迷失在这里,希望有人可以帮忙。
以下是C#脚本的片段:
public void Main()
{
// TODO: Add your code here
try
{
// Ignore certificate warnings
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(delegate { return true; });
// Disable server certificate validation.
//ServicePointManager
// .ServerCertificateValidationCallback +=
// (sender, cert, chain, sslPolicyErrors) => true;
//ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
//System.Net.ServicePointManager.ServerCertificateValidationCallback =
//delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
//{ return true; };
// Logging start of download
bool fireAgain = true;
Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Connections["TEST"].ConnectionString, string.Empty, 0, ref fireAgain);
// Get your newly added HTTP Connection Manager
Object mySSISConnection = Dts.Connections["TEST"].AcquireConnection(null);
// Create a new connection
HttpClientConnection myConnection = new HttpClientConnection(mySSISConnection);
// Download file and use the Flat File Connectionstring (D:\SourceFiles\Products.csv)
// to save the file (and replace the existing file)
String filename = Dts.Variables["varFullPathScript"].Value.ToString();
myConnection.DownloadFile(filename, true);
// Logging end of download
Dts.Events.FireInformation(0, "Download File", "Finished downloading" , string.Empty, 0, ref fireAgain);
// Quit Script Task succesful
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
// Logging why download failed
Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);
// Quit Script Task unsuccesful
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
答案 0 :(得分:1)
经过一番搔痒之后,我想我会尝试一些简单的东西,使用不同的方法来下载文件。我没有使用HTTP连接管理器,而是使用了webclient类。 这似乎有效!
WebClient webClient = new WebClient();
//webClient.Credentials = new NetworkCredential("username", "PW", "domain"); //don't really need this unless URL requires auth
webClient.DownloadFile(url, filename);
答案 1 :(得分:0)
@Jonathan C - 首先,谢谢你的想法,我遇到了同样的问题,你救了我一天,但是
TLS 1.0 现在默认禁用,所以我有一个 “底层连接已关闭:发送时发生意外错误” 异常,直到在调用之前添加此字符串System.Net.ServicePointManager.SecurityProtocol=System.Net.SecurityProtocolType.Tls12;
我建议在这种情况下使用 using
关键字
所以我的最终结果是
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
using (var webClient = new WebClient())
{
webClient.DownloadFile(@"https://url", @"C:\file");
}