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?