Login to website and use cookie to get source for another page

后端 未结 1 1026
暖寄归人
暖寄归人 2020-12-07 16:14

I am trying to login to the TV Rage website and get the source code of the My Shows page. I am successfully logging in (I have checked the response from my post request) bu

相关标签:
1条回答
  • 2020-12-07 16:31

    Here's a simplified and working version of your code using a WebClient:

    class Program
    {
        static void Main()
        {
            var shows = GetSourceForMyShowsPage();
            Console.WriteLine(shows);
        }
    
        static string GetSourceForMyShowsPage()
        {
            using (var client = new WebClientEx())
            {
                var values = new NameValueCollection
                {
                    { "login_name", "xxx" },
                    { "login_pass", "xxxx" },
                };
                // Authenticate
                client.UploadValues("http://www.tvrage.com/login.php", values);
                // Download desired page
                return client.DownloadString("http://www.tvrage.com/mytvrage.php?page=myshows");
            }
        }
    }
    
    /// <summary>
    /// A custom WebClient featuring a cookie container
    /// </summary>
    
    public class WebClientEx : WebClient
    {
        public CookieContainer CookieContainer { get; private set; }
    
        public WebClientEx()
        {
            CookieContainer = new CookieContainer();
        }
    
        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = CookieContainer;
            }
            return request;
        }
    }
    
    0 讨论(0)
提交回复
热议问题