Log in and use a cookie to get the source for another page.

I’m trying to log in to TV Rage and get the source code on the My Shows page. I successfully log in (I checked the response from my send request), but then when I try to execute a receive request on the My Shows page, I get redirected to the login page.

This is the code I use to login:

    private string LoginToTvRage()
    {
        string loginUrl = "http://www.tvrage.com/login.php";
        string formParams = string.Format("login_name={0}&login_pass={1}", "xxx", "xxxx");
        string cookieHeader;
        WebRequest req = WebRequest.Create(loginUrl);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();
        cookieHeader = resp.Headers["Set-cookie"];
        String responseStream;
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            responseStream = sr.ReadToEnd();
        }
        return cookieHeader;
    }

Then I pass cookieHeaderto this method, which should be the source of the My Shows page:

    private string GetSourceForMyShowsPage(string cookieHeader)
    {
        string pageSource;
        string getUrl = "http://www.tvrage.com/mytvrage.php?page=myshows";
        WebRequest getRequest = WebRequest.Create(getUrl);
        getRequest.Headers.Add("Cookie", cookieHeader);
        WebResponse getResponse = getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }
        return pageSource;
    }

I used this previous question as a guide, but I don't understand why my code is not working.

+5
source share
1

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;
    }
}
+17

All Articles