0

I am trying to download a zip file in SSIS script task using the following code below. However, I am getting the error:

[] Error: The underlying connection was closed: An unexpected error occurred on a send.

The file I am trying to download is: https://www.sam.gov/public-extracts/SAM-Public/SAM_Exclusions_Public_Extract_16229.ZIP

Any help would be appreciated

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;
    }
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Are you behind a firewall that could be blocking? – Stinky Towel Aug 17 '16 at 01:31
  • No...everything was working fine but recently I believe the government upgraded to TLS 1.2. Since then this script has been failing – Michael Miller Aug 17 '16 at 01:38
  • Are you able to DL this file on the same machine via a web browser? – Stinky Towel Aug 17 '16 at 01:43
  • Yes. I am able to browse to the URL using Chrome, Firefox, or IE and download the file without any issues – Michael Miller Aug 17 '16 at 01:44
  • Hmm feels firewallish, but I don't know your network. Some things to try are: 1) set a user-agent for your client that matches your browser 2) don't override the cert validation callback and check for ssl errors and/or cert chain issues, 3) run netmon (if you can) and check the TLS/SSL log entries 4) verify the default credentials are allowed to access this URL – Stinky Towel Aug 17 '16 at 01:53
  • 1
    I fixed the issue by adding ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; after ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); – Michael Miller Aug 17 '16 at 01:54
  • Glad to hear you got it working! – Stinky Towel Aug 17 '16 at 02:08

0 Answers0