使用HttpWebRequest发送Cookie

本文关键字:Cookie 发送 HttpWebRequest 使用 | 更新日期: 2023-09-27 18:00:22

我正试图登录我经常访问的网站中的Ajax聊天室。我想创建一个类似的审核机器人,但我对cookie处理很感兴趣。我已经搜索了这个网站上的所有问题,但所有的解决方案似乎都与我正在做的完全一样,即将HttpWebRequestCookieContainer参数设置为ccCookieContainer中填充了数据,但该数据不会与HttpWebRequest一起发送。我的代码如下所示。

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

在不亲自手工制作标题的情况下,如何使用HttpWebRequest发送cookie?我做错了什么?

使用HttpWebRequest发送Cookie

在这里使用Justin和Rajeesh的答案:将CookieContainer与WebClient类一起使用,我手动发送cookie,如下所示:

        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();