使用HttpWebRequest发送Cookie

时间:2015-06-30 04:08:51

标签: c# ajax cookies httpwebrequest

我正在尝试登录我经常访问的网站中的Ajax聊天室。我希望创建一个适合的审核机器人,但我挂起了cookie处理。我搜索了本网站上的所有问题,但所有解决方案似乎都正是我正在做的事情,即将CookieContainer HttpWebRequest参数设置为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可以做些什么而不用自己手动制作标题,我做错了什么?

1 个答案:

答案 0 :(得分:1)

在这里使用Justin和Rajeesh的答案:Using CookieContainer with WebClient class我手动发送了这样的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();