C# WebClient登录与下载文件,带cookie

public class WebClientEx : WebClient
        {
            private CookieContainer cookie = new CookieContainer();
            protected override WebRequest GetWebRequest(Uri address)
            {
                WebRequest request = base.GetWebRequest(address);
                if (request is HttpWebRequest)
                {
                    (request as HttpWebRequest).CookieContainer = cookie;
                }
                return request;
            }
        }
NameValueCollection postValues = new NameValueCollection();
                postValues.Add("Email", info.username);
                postValues.Add("Pwd", info.userpwd);
                WebClientEx client = new WebClientEx();
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                Encoding enc = Encoding.GetEncoding("UTF-8");
                client.UploadValues(login, "POST", postValues);
                client.DownloadFile(file, path);

猜你喜欢

转载自blog.csdn.net/liangyely/article/details/106289185
今日推荐