登录网站并使用cookie获取其他页面的来源

时间:2010-05-09 18:10:01

标签: c# .net http cookies

我正在尝试登录TV Rage网站并获取“我的节目”页面的源代码。我成功登录(我已经检查了我的帖子请求的响应)但是当我尝试在“我的显示”页面上执行获取请求时,我被重定向到登录页面。

这是我用来登录的代码:

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

然后我将cookieHeader传递给此方法,该方法应该是My Shows页面的来源:

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

我一直在使用this previous question作为指导,但我不知道为什么我的代码无效。

1 个答案:

答案 0 :(得分:16)

以下是使用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;
    }
}