Send Cookies With HttpWebRequest [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-24 17:29:27

问题


I am attempting to log in to an Ajax chatroom in a website I often visit. I desire to create a moderation bot of sorts, but I am hung up on cookie handling. I have searched through all of the questions on this website, but all solutions seem to do exactly what I am doing, which is setting the CookieContainer parameter of HttpWebRequest to cc. The CookieContainer gets populated with data, but this data does not get sent with the HttpWebRequest. My code is shown below.

    class Program
{
    static config populated_config;
    static void Main(string[] args)
    {
        #region config
        StreamReader sr = new StreamReader(File.Open("config.xml", FileMode.Open), Encoding.UTF8);
        XmlSerializer xmls = new XmlSerializer(typeof(config));
        populated_config = (config)xmls.Deserialize(sr);
        #endregion

        #region login

        //retrieve default cookies
        CookieContainer cc = new CookieContainer();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(populated_config.domain + "chat/");
        request.CookieContainer = cc;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        string login_info = "login=login&redirect=http%3A%2F%2F"+WebUtility.UrlEncode(populated_config.domain)+"%2Fchat%2F%3FchannelName%3DPublic&username=" + WebUtility.UrlEncode(populated_config.username) + "&password=" + WebUtility.UrlEncode(populated_config.password) + "&channelName=Public&lang=en&submit=Login";
        request = (HttpWebRequest)WebRequest.Create(populated_config.domain + "ucp.php?mode=login");
        request.CookieContainer = cc;
        request.Method = "POST";
        StreamWriter sw = new StreamWriter(request.GetRequestStream());
        sw.Write(login_info);
        response = (HttpWebResponse)request.GetResponse();

        string sid = findCookieValue(cc, "phpbb3_jznvi_sid");
        request = (HttpWebRequest)WebRequest.Create(populated_config.domain + "chat/?channelName=Public&sid=" + sid);
        request.CookieContainer = cc;
        request.Method = "GET";
        response = (HttpWebResponse)request.GetResponse();
        #endregion
    }

    public static string findCookieValue(CookieContainer cc,string cookieName)
    {
        foreach (Cookie cookie in cc.GetCookies(new Uri(populated_config.domain)))
            if (cookie.Name == cookieName)
                return cookie.Value;
        return null;
    }
}

What can be done to send the cookies with the HttpWebRequest without manually crafting the header myself, and what am I doing incorrectly?


回答1:


Using Justin and Rajeesh's answer here: Using CookieContainer with WebClient class I sent the cookies manually like this:

        string login_info = "login=login&redirect=http%3A%2F%2F"+WebUtility.UrlEncode(populated_config.domain)+"%2Fchat%2F%3FchannelName%3DPublic&username=" + WebUtility.UrlEncode(populated_config.username) + "&password=" + WebUtility.UrlEncode(populated_config.password) + "&channelName=Public&lang=en&submit=Login";
        request = (HttpWebRequest)WebRequest.Create(populated_config.domain + "ucp.php?mode=login");
        request.Method = "POST";
        //manually populate cookies
        Console.WriteLine(cc.GetCookieHeader(new Uri(populated_config.domain)));
        request.Headers.Add(HttpRequestHeader.Cookie,cc.GetCookieHeader(new Uri(populated_config.domain)));
        StreamWriter sw = new StreamWriter(request.GetRequestStream());
        sw.Write(login_info);
        response = (HttpWebResponse)request.GetResponse();


来源:https://stackoverflow.com/questions/31129381/send-cookies-with-httpwebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!