C#HttpWebRequest使用cookie

时间:2018-09-19 12:23:26

标签: c# httpwebrequest httpwebresponse httpcookiecollection

我在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 on webSite

只能,我无法在网站上导航,最终进入登录页面,cookie不好,并且我处于调试状态,我只加载了三个(LLCookie)中的一个cookie(BrowseSettings)。和LLTZCookie):

Debug cookie C#

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

1 个答案:

答案 0 :(得分:0)

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

request.AllowAutoRedirect = true;