1
我試圖登錄到我經常訪問的網站中的Ajax聊天室。我希望創建一個適中的機器人,但我被掛在cookie處理上。我已經搜索了本網站上的所有問題,但是所有解決方案似乎完全按照我的工作進行,即將CookieContainer
參數HttpWebRequest
設置爲cc
。 CookieContainer
會填充數據,但這些數據不會與HttpWebRequest
一起發送。我的代碼如下所示。使用HttpWebRequest發送Cookie
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
餅乾,而無需手動各具特色的頭我自己,我在做什麼錯誤?