使用Cookie身份验证自动化WebClient()DownloadFile [英] Automating WebClient() DownloadFile With Cookie Authentication

查看:106
本文介绍了使用Cookie身份验证自动化WebClient()DownloadFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好。 


想知道是否有人可以帮助我查询我的问题。我创建了一个脚本任务,每小时从
下载文件
web-portal 。现在,客户最近将
安全 设置更改为Web -portal所以我必须在我的脚本任务中添加Cookie用户名和密码。下面的包工作正常,并为我下载文件。但是,
问题是我每天都在我的脚本任务中手动更新Cookie信息(密码)(原因是密码在8-10小时后到期)。所以我会在早上打开门户网站并登录,这将更新浏览器历史记录
中的Cookie信息,然后我会从浏览器检查密码,然后在下面的脚本中更新密码。现在我的问题是,有没有办法可以自动执行此cookie检查/更新脚本步骤。非常感谢您的帮助。



   
public
void Main()


  
      {

        {


<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
// TODO:在这里添加代码



           
WebClient wc =

< span style ="font-size:9.5pt; font-family:Consolas; color:#2B91AF; background:white"> WebClient (); //
{ UseDefaultCredentials = true};


         
wc.Headers.Add(
HttpRequestHeader .Cookie,
" CookieName = CookiePassword" );

          wc.Headers.Add(HttpRequestHeader.Cookie, "CookieName=CookiePassword");


          ;&NBSP;&NBSP;
var DownloadPath = Dts.Variables [ " User :: varDownloadPathNew" ]。 .ToString();



           
wc.Credentials =
new
NetworkCredential " UserName"
"密码"
" 123.co.uk" );

            wc.Credentials = new NetworkCredential("UserName", "Password", "123.co.uk");


           
wc.DownloadFile(
" https://123.co.uk/model/jobsdownload?mnu_grade1 = true& 70106f432b38& clientid =& clientpersonid =& additionalrecordpks =& selectedjobid =& separator =%C2%A6& maxrows = 400& format = excel& filename = Test.xls"
DownloadPath);

            wc.DownloadFile("https://123.co.uk/model/jobsdownload?mnu_grade1=true& 70106f432b38&clientid=&clientpersonid=&additionalrecordpks=&selectedjobid=&separator=%C2%A6&maxrows=400&format=excel&filename=Test.xls", DownloadPath);



           
Dts.TaskResult =(
int ScriptResults 。成功;

            Dts.TaskResult = (int)ScriptResults.Success;


       
}

        }

推荐答案

嗨  mumtaz_khurram81,

Hi mumtaz_khurram81,

感谢您在此发帖。

对于您的问题,我建议您这样做以下步骤。

For your question, I suggest you to do the steps below.

步骤1:获取新的cookie并用它来替换旧的。

步骤 2 :您可以将密码保存到文件中,然后通过它更新cookie。

Step1: Get the new cookie and use it to replace the old.
Step2: You can save the password to a file and then update the cookie by it.

请参考下面的代码。

            /// <summary>
            /// </summary>
            /// <param name="cookie1">Cookie1</param>
            /// <param name="cookie2">Cookie2</param>

            internal static string GetMergeCookie(string cookie1, string cookie2)
            {
                if (string.IsNullOrWhiteSpace(cookie1))//The New is null
                {
                    return cookie2;//return the oldcookie
                }
                if (string.IsNullOrWhiteSpace(cookie2))//the old is null
                {
                    return cookie1;//return the newcookie
                }
                List<string> cookielist = new List<string>();//result
                string[] list_1 = cookie1.ToString().Split(';');
                string[] list_2 = cookie2.ToString().Split(';');
                foreach (string item in list_1)
                {
                //Exclude duplicate items 
                if (cookielist.Contains(item)) continue;
                    cookielist.Add(string.Format("{0} ", item));
                }
                foreach (string item in list_2)
                {
                //Exclude duplicate items 
                if (cookielist.Contains(item)) continue;
                    cookielist.Add(string.Format("{0}", item));
                }
                return string.Join(";", cookielist);
            }

最好的问候,

Wendy


这篇关于使用Cookie身份验证自动化WebClient()DownloadFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆