使用HttpWebRequest发送Cookie [英] Send Cookies With HttpWebRequest
问题描述
我试图登录到我经常访问的网站中的Ajax聊天室。我希望创建各种审核机器人,但是我对Cookie的处理很感兴趣。我已经搜索了该网站上的所有问题,但是所有解决方案似乎都可以正确地执行我的操作,即设置的
到 CookieContainer
参数HttpWebRequest cc
。 CookieContainer
填充了数据,但此数据未与 HttpWebRequest
发送。我的代码如下所示。
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;
}
}
使用 HttpWebRequest
而不自己亲自制作标题,我做错了什么?
What can be done to send the cookies with the HttpWebRequest
without manually crafting the header myself, and what am I doing incorrectly?
推荐答案
在这里使用Justin和Rajeesh的答案:在WebClient类中使用CookieContainer 我发送了cookie手动像这样:
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();
这篇关于使用HttpWebRequest发送Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!