问题
I've created a SSIS package which downloads a CSV file from a HTTPS URL using C# script. Everything works fine when executed from Visual Studio.
However, when I created a SQL Agent job the package fails. The package also fails if I execute the .dtsx file directly from the SQL Server.
The error is
"Download failed: SSL certificate response obtained from the server was not valid. Cannot process the request"
I've tested opening the URL from the SQL server and can view the file. I've also added code in the C# script to ignore certification errors but this doesn't seem to have any impact. I've also entered my login credentials for the server hosting the CSV in the Connection Manager but that didn't help as well.
The CSV is generated via Magento Data feed. On the website everything is redirected to HTTPS.
I'm at a lost here so hopefully someone can kindly assist.
Below is snippet of the C# script:
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;
}
回答1:
After some head scratching I thought I'd try something simple which is using a different method to download the file. Instead of the HTTP connection manager I've used a webclient class. This seems to have worked!
WebClient webClient = new WebClient();
//webClient.Credentials = new NetworkCredential("username", "PW", "domain"); //don't really need this unless URL requires auth
webClient.DownloadFile(url, filename);
来源:https://stackoverflow.com/questions/43495703/ssis-download-from-http-error-ssl-certification-response-obtained-from-server