C# CookieContainer不';不要保存所有的饼干
我正在使用WebClient登录Wordpress博客,我的Web客户端方法:C# CookieContainer不';不要保存所有的饼干,c#,C#,我正在使用WebClient登录Wordpress博客,我的Web客户端方法: protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request.GetType() == typeof(HttpWebRequest)) { ((HttpWeb
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request.GetType() == typeof(HttpWebRequest))
{
((HttpWebRequest)request).CookieContainer = cookieContainer;
foreach (Cookie c in cookieContainer.GetCookies(address))
{
Console.WriteLine(" cName: " + c.Name);
Console.WriteLine(" cValue: " + c.Value);
Console.WriteLine(" cPath: " + c.Path);
Console.WriteLine(" cDomain: " + c.Domain);
}
((HttpWebRequest)request).UserAgent = userAgent;
((HttpWebRequest)request).Timeout = timeout;
((HttpWebRequest)request).Accept = accept;
}
return request;
}
[DebuggerNonUserCode()]
protected override WebResponse GetWebResponse(WebRequest request)
{
try
{
WebResponse response = base.GetWebResponse(request);
if (response.GetType() == typeof(HttpWebResponse))
{
foreach (Cookie c in ((HttpWebResponse)response).Cookies)
{
Console.WriteLine(" Name: " + c.Name);
Console.WriteLine(" Value: " + c.Value);
Console.WriteLine(" Path: " + c.Path);
Console.WriteLine(" Domain: " + c.Domain);
}
characterSet = ((HttpWebResponse)response).CharacterSet;
}
return response;
}
catch (System.Net.WebException e)
{
throw e;
}
}
现在,当我尝试登录wordpress时,发送四个cookies和302重定向
Set-Cookie: wordpress_a235c74829f55a618a01c9f088805f08=precmast%7C1318622660%7C45c78363a5062b592b1fe49201fea5a8; path=/wp-content/plugins; httponly
Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/
Set-Cookie: wordpress_a235c74829f55a618a01c9f088805f08=precmast%7C1318622660%7C45c78363a5062b592b1fe49201fea5a8; path=/wp-admin; httponly
Set-Cookie: wordpress_logged_in_a235c74829f55a618a01c9f088805f08=precmast%7C1318622558%7Ca28c4bf14832cbbee606cdddcad9e019; path=/; httponly
但是WebResponse只包含2个cookie(路径=/),当它自动重定向时,它不会再发送2个cookie,因此我无法登录。现在我通过手动处理response.Headers[“Set Cookie”]和response.Headers[“Location”]中的重定向和Cookie解决了这个问题。但我想知道还有别的解决办法吗
实际修复
if (response.GetType() == typeof(HttpWebResponse))
{
if(response.Headers["Location"] != null)
{
cookieContainer.Add(response.ResponseUri, GetAllCookiesFromHeader(response.Headers["Set-Cookie"], response.ResponseUri.Host));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(response.Headers["Location"]);
req.Method = "GET";
req.AllowAutoRedirect = false;
req.CookieContainer = cookieContainer;
req.UserAgent = userAgent;
req.Timeout = timeout;
req.Accept = accept;
return GetWebResponse(req);
}
试试这个:
request.AllowAutoRedirect = false;
您要做的是:
- 查找登录时发送到wordpress站点的“帖子”。
- firefox的“Live HTTP Headers”插件非常适合签出头李>
- 查看帖子信息
- 然后使用HttpWebRequest重新发送该信息。
- 向请求中添加一个真实的UserAgent字符串是一个好主意,这样您看起来就像一个真实的人
- 没有那么重要,但是在请求上有一个逻辑参考者也不是一个坏主意
- 将CookieContainer附加到请求以获取登录cookie李>
- 为了避免每次启动程序时都必须执行此操作:
- 您可以序列化和反序列化CookieContainer
- 对于序列化,我个人成功地使用了BinaryFormatter
我不确定提供代码示例是否具有建设性。有了这些信息,了解如何自己编写代码比复制和粘贴更有意义。但是,如果您想要其中任何一个的代码示例,请留下评论。正如我所说的,我正在使用这个-我在GetWebResponse中手动处理cookies和重定向