WebClient帶Cookie 保持Session

WebClient、Cookie、Session

當使用WebClient進行網頁爬蟲時,有登入行為的話下一次在使用WebClient時卻是沒有登入的狀態,

這是因為WebClient沒有辦法保持上次的cookie

這時候就要將WebClient進行複寫,使用CookieContainer 讓WebClient保持登入的session

public class CookieAwareWebClient : WebClient
{
    public CookieContainer CookieContainer { get; set; } = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest request = base.GetWebRequest(uri);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = CookieContainer;
        }
        return request;
    }
}