从HTTPS网址下载文件

时间:2016-08-17 00:31:46

标签: c#

我正在尝试使用以下代码在SSIS脚本任务中下载zip文件。但是,我收到了错误:

  

[]错误:基础连接已关闭:意外错误   在发送时发生。

我要下载的文件是:https://www.sam.gov/public-extracts/SAM-Public/SAM_Exclusions_Public_Extract_16229.ZIP

任何帮助将不胜感激

public void Main()
{      
    WebClient myWebClient;
    string RemoteURI;
    string LocalFileName;
    bool FireAgain = true;

    Variables vars = null;
    Dts.VariableDispenser.LockForRead("User::vSSOReportURL");
    Dts.VariableDispenser.LockForRead("User::vSSOLocalFileName");
    Dts.VariableDispenser.LockForWrite("User::vSSOReportURLIndicator");
    Dts.VariableDispenser.GetVariables(ref vars);

    try
    {
        // Ignore certificate warnings
        ServicePointManager.ServerCertificateValidationCallback = 
             new RemoteCertificateValidationCallback(delegate { return true; });

        // Initiate webclient download, use default credentials (current login)
        myWebClient = new WebClient();
        myWebClient.Credentials = CredentialCache.DefaultCredentials; 

        RemoteURI = vars["User::vSSOReportURL"].Value.ToString();
        LocalFileName = vars["User::vSSOLocalFileName"].Value.ToString();


        // Log provider notification 
        Dts.Events.FireInformation(0, String.Empty, String.Format("Downloading '{0}' from '{1}'", 
        LocalFileName, RemoteURI), String.Empty, 0, ref FireAgain);

        // Download the file 
        myWebClient.DownloadFile("https://www.sam.gov/public-extracts/SAM-Public/SAM_Exclusions_Public_Extract_16229.ZIP", @"c:\temp\SAM_Exclusions_Public_Extract_16229.ZIP");

        // Set report URL indicator, this is used to determine the http source of the 
        // download i.e. vSSOReportURL or vSSOReportURLRetry for the message which is 
        // written to the table
        vars["User::vSSOReportURLIndicator"].Value = 0;

        // Return success
        Dts.TaskResult = (int)ScriptResults.Success;
    }

    catch (Exception ex)
    {
        // Catch and handle error 
        Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

0 个答案:

没有答案