Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录网站并使用cookie获取其他页面的源代码_C#_.net_Http_Cookies - Fatal编程技术网

C# 登录网站并使用cookie获取其他页面的源代码

C# 登录网站并使用cookie获取其他页面的源代码,c#,.net,http,cookies,C#,.net,Http,Cookies,我正在尝试登录到TV Rage网站并获取我的节目页面的源代码。我正在成功登录(我已经检查了post请求的响应),但是当我尝试在“我的节目”页面上执行get请求时,我被重新定向到登录页面 这是我用来登录的代码: private string LoginToTvRage() { string loginUrl = "http://www.tvrage.com/login.php"; string formParams = string.Format("

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

这是我用来登录的代码:

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

我一直在使用它作为指导,但我不知道为什么我的代码不起作用。

下面是一个简化的、有效的代码版本,它使用了:

类程序
{
静态void Main()
{
var shows=GetSourceForMyShowsPage();
控制台写入线(显示);
}
静态字符串GetSourceForMyShowsPage()
{
使用(var client=new WebClientEx())
{
var values=新的NameValueCollection
{
{“登录名”,“xxx”},
{“登录密码”,“xxxx”},
};
//鉴定
client.uploadValue(“http://www.tvrage.com/login.php“、价值观);
//下载所需页面
返回client.DownloadString(“http://www.tvrage.com/mytvrage.php?page=myshows");
}
}
}
/// 
///具有cookie容器的自定义WebClient
/// 
公共类WebClient-Tex:WebClient
{
公共CookieContainer CookieContainer{get;private set;}
公共WebClientEx()
{
CookieContainer=新CookieContainer();
}
受保护的覆盖WebRequest GetWebRequest(Uri地址)
{
var request=base.GetWebRequest(地址);
if(请求为HttpWebRequest)
{
(请求为HttpWebRequest)。CookieContainer=CookieContainer;
}
返回请求;
}
}

这太完美了!非常感谢你!
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;
    }
}