这个问题已经在这里有一个答案:
- 使用的CookieContainer与WebClient类 5个回答
我试图登录到一个网站,我经常访问一个Ajax聊天室。 我渴望创造各种各样的适度机器人,但我挂了cookie处理。 我已经通过所有本网站的问题搜索,但所有的解决方案似乎做什么我做的,这是设置CookieContainer
的参数HttpWebRequest
至cc
。 该CookieContainer
被填充数据,但这个数据没有得到与发送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
无需手动各具特色的头我自己,我在做什么错误?