简体   繁体   English

SSIS WebClient()下载文件脚本任务问题

[英]SSIS WebClient() Download File script Task issue

I am wondering if someone could help me with this issue please. 我想知道是否有人可以帮助我解决这个问题。 I am new to using script tasks in SSIS packages and i am unable to find solution to this issue. 我是SSIS包中使用脚本任务的新手,我无法找到此问题的解决方案。 So i created this script Task to download a file from Web-portal. 因此,我创建了此脚本Task来从Web门户下载文件。 Package was working fine for couple of months but all of sudden, it started failing. Package正常工作了几个月,但突然间,它开始失败。 Reason is that , url is being directed to Authentication page of Web-Portal again (looks like Client might have changed the security settings of their website or something). 原因是,URL再次被定向到Web-Portal的“身份验证”页面(看来客户端可能已更改了其网站的安全设置或其他内容)。 So now a blank file is being downloaded. 因此,现在正在下载一个空白文件。 below is the code i am using in my script task. 以下是我在脚本任务中使用的代码。 Is there a code i can add to below script to by-pass the authentication page as i am already sending username and password in below script. 我是否可以在下面的脚本中发送用户名和密码,是否可以向下面的脚本添加代码以绕过身份验证页面。 One more thing to add, i can download the file manually when i copy and paste url in to Chrome so that means data file does exist in portal,it's just the script task being re-directed to authentication page again thus failing. 要添加的另一件事是,当我将URL复制并粘贴到Chrome中时,我可以手动下载文件,这意味着数据文件确实存在于门户中,只是脚本任务再次被重定向到身份验证页面,从而失败了。 Thanks in advance. 提前致谢。

    public void Main()
    {
        // TODO: Add your code here

        WebClient wc = new WebClient();// { UseDefaultCredentials = true };
        var DownloadPath = Dts.Variables["User::varDownloadPathNew"].Value.ToString();
        DateTime startDate = DateTime.Parse(Dts.Variables["User::StartDate"].Value.ToString());
        DateTime enddate = DateTime.Parse(Dts.Variables["User::EndDate"].Value.ToString());

        wc.Credentials = new NetworkCredential("UserName", "Password");
        wc.DownloadFile("https://Test123.co.uk/model/download?&startfilter=" + startDate.ToString("dd") + "%2F" + startDate.ToString("MM") + "%2F" + startDate.ToString("yyyy") + "&mnu_jobdateendfilter=" + enddate.ToString("dd") + "%2F" + enddate.ToString("MM") + "%2F" + enddate.ToString("yyyy") + "&%A6&maxrows=400&format=excel&filename=Test.xls", DownloadPath);

        Dts.TaskResult = (int)ScriptResults.Success;

    }

Adding below code to my original code resolved the issue. 在我的原始代码中添加以下代码即可解决此问题。 As suggested by @billinkc it, portal was using cookies to Authenticate, so i checked cookie user name and password from ie/chrome (in my case, portal was using 2 cookie names and passwords so i used both) and then used it in below code wc.Headers.Add(HttpRequestHeader.Cookie,"cookiename=password"); 正如@billinkc所建议的那样,门户网站正在使用Cookie进行身份验证,因此我从ie / chrome中检查了cookie用户名和密码(在我的情况下,门户网站使用了2个cookie名称和密码,因此我同时使用了两者),然后在下面使用了它代码wc.Headers.Add(HttpRequestHeader.Cookie,“ cookiename = password”);

public void Main()
{
    // TODO: Add your code here

    WebClient wc = new WebClient();// { UseDefaultCredentials = true };
    wc.Headers.Add(HttpRequestHeader.Cookie, "NSC_JOmbbd3tb4=ffffffffc3a03f7e45525; User=eyJhbGciOiJSU");
    var DownloadPath = Dts.Variables["User::varDownloadPathNew"].Value.ToString();
    DateTime startDate = DateTime.Parse(Dts.Variables["User::StartDate"].Value.ToString());
    DateTime enddate = DateTime.Parse(Dts.Variables["User::EndDate"].Value.ToString());

    wc.Credentials = new NetworkCredential("UserName", "Password");
    wc.DownloadFile("https://Test123.co.uk/model/download?&startfilter=" + startDate.ToString("dd") + "%2F" + startDate.ToString("MM") + "%2F" + startDate.ToString("yyyy") + "&mnu_jobdateendfilter=" + enddate.ToString("dd") + "%2F" + enddate.ToString("MM") + "%2F" + enddate.ToString("yyyy") + "&%A6&maxrows=400&format=excel&filename=Test.xls", DownloadPath);

    Dts.TaskResult = (int)ScriptResults.Success;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM