0

我在 C# 中处理 cookie 有一点问题

所以在我的网站上,我有一个登录页面,一旦登录,我就会被重定向到主页。我使用 HttpWebRequest 连接并遵循重定向,我创建了一个类,这里​​是:

class webReq
{
    private string urlConnection;
    private string login;
    private string password;
    private CookieCollection cookieContainer;
    private long executionTime = 0;



    public webReq(string urlCo, string login, string pass)
    {
        this.urlConnection = urlCo;
        this.login = login;
        this.password = pass;
        this.cookieContainer = null;
    }

    public void StartConnection()
    {
        string WriteHTML = "D:/REM/Connection.html";

        List<string> datas = new List<string>();
        datas.Add("Username=" + this.login);
        datas.Add("Password=" + this.password);
        datas.Add("func=ll.login");
        datas.Add("NextURL=/admin/livelink.exe");
        datas.Add("loginbutton=Sign in");
        string postData = "";
        postData = string.Join("&", datas);
        var buffer = Encoding.ASCII.GetBytes(postData);

        try
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.urlConnection);
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1003.1 Safari/535.19";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = buffer.Length;
            request.CookieContainer = new CookieContainer();

            Stream stream = request.GetRequestStream();
            stream.Write(buffer, 0, buffer.Length);
            stream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            stream = response.GetResponseStream();
            watch.Stop();
            this.executionTime = watch.ElapsedMilliseconds;

            StreamReader reader = new StreamReader(stream);
            System.IO.File.WriteAllText(WriteHTML, reader.ReadToEnd());
            this.cookieContainer = new CookieCollection();
            foreach (Cookie cookie in response.Cookies)
            {
                this.cookieContainer.Add(cookie);
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.GetBaseException().ToString());
        }
    }
}

我很好地加载了主页,并且设法获得了一个 cookie。

所以我开发了一个功能来使用我的cookie来浏览网站:

    public void connectUrl(string url, int numeroTest)
    {
        string WriteHTML = "D:/REM/Page"+numeroTest+".html";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        //Add cookie to request.CookieContainer
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(this.cookieContainer);
        var watch = System.Diagnostics.Stopwatch.StartNew();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        watch.Stop();
        this.executionTime = watch.ElapsedMilliseconds;
        StreamReader reader = new StreamReader(stream);
        System.IO.File.WriteAllText(WriteHTML, reader.ReadToEnd());

    }

通常,我必须检索三个 cookie,例如在网站上: 网站上的 Cookie

只是,我无法在网站上导航,最终进入登录页面,cookie 不好,而且我正在调试,我只加载了三个 cookie(浏览设置)(LLCookie 和 LLTZCookie) :

调试 cookie C#

我不明白为什么我无法检索网站上的所有 cookie....如果有人有解决方案!

4

1 回答 1

0

我在我的 StartConnection() 方法中找到了无法获取所有 cookie 的原因,即使我无法通过禁用重定向找到它的确切原因:

request.AllowAutoRedirect = true;
于 2018-09-20T06:45:12.197 回答