这两天做了一个获取cookie并且携带此cookie去请求另外一个url地址,中间携带cookie用了两种方式:
1. httpRequest.CookieContainer= cookie (此cookie为一个cookie容器对象)
2.httpRequest.Headers.Add("Cookie", cookie) (此cookie为一个cookie字符串)
测试结果:1种方式cookie失效并且丢失。2种方式携带成功并且可以成功显示已登录。
原因待查,记录下。
说明:也可能cookie容器设置的方式有问题。
附上代码:
public class Login
{
public string GetCookie(string postString, string postUrl)
{
CookieContainer cookie = new CookieContainer();
HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//创建http 请求
httpRequset.CookieContainer = cookie;//设置cookie
httpRequset.Method = "POST";//POST 提交
httpRequset.KeepAlive = true;
httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36";
httpRequset.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpRequset.ContentType = "application/x-www-form-urlencoded";//以上信息在监听请求的时候都有的直接复制过来
httpRequset.Referer = "http://my.qianlima.com/login.jsp";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString);
httpRequset.ContentLength = bytes.Length;
Stream stream = httpRequset.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();//以上是POST数据的写入
HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();//获得 服务端响应
var str = cookie.GetCookieHeader(httpRequset.RequestUri);
return str;//拿到cookie
}
public string GetContent(string cookie, string url)
{
string content;
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpRequest.Headers.Add("Cookie", cookie);
httpRequest.Referer = url;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36";
httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.Method = "GET";
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (Stream responsestream = httpResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.Default))
{
content = sr.ReadToEnd();
}
}
return content;
}
}
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。 | 译文 原文 |
CookieContainer 类
.NET Framework 4.6 and 4.5 其他版本

为 CookieCollection 对象的集合提供容器。
命名空间: System.Net
程序集: System(System.dll 中)
继承层次结构 System::Object
System.Net::CookieContainer
语法 C++
[SerializableAttribute]
public ref class CookieContainer
构造函数
名称 | 说明 | |
---|---|---|
| CookieContainer() | 初始化 CookieContainer 类的新实例。 |
| CookieContainer(Int32) | 使用指定的表示容器可以包含的 Cookie 实例数的值初始化 CookieContainer 类的新实例。 |
| CookieContainer(Int32, Int32, Int32) | 使用指定属性初始化 CookieContainer 类的新实例。 |
属性
名称 | 说明 | |
---|---|---|
| Capacity | 获取和设置 CookieContainer 可以包含的 Cookie 实例数。 |
| Count | 获取 CookieContainer 当前包含的 Cookie 实例数。 |
| MaxCookieSize | 表示 Cookie 的最大允许长度。 |
| PerDomainCapacity | 获取和设置 CookieContainer 可以在每个域包含的 Cookie 实例数。 |
方法
名称 | 说明 | |
---|---|---|
| Add(Cookie^) | 将 Cookie 添加到 CookieContainer。此方法使用 Cookie 中的域来确定将 Cookie 与哪个域集合相关联。 |
| Add(CookieCollection^) | 将 CookieCollection 的内容添加到 CookieContainer 中。 |
| Add(Uri^, Cookie^) | 将 Cookie 添加到特定 URI 的 CookieContainer 中。 |
| Add(Uri^, CookieCollection^) | 将 CookieCollection 的内容添加到特定 URI 的 CookieContainer 中。 |
| Equals(Object^) | 确定指定的对象是否等于当前对象。(从 Object 继承。) |
| Finalize() | 在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从 Object 继承。) |
| GetCookieHeader(Uri^) | 获取 HTTP Cookie 标头,该标头包含表示与特定 URI 关联的 Cookie 实例的 HTTP Cookie。 |
| GetCookies(Uri^) | 获取包含与特定 URI 关联的 Cookie 实例的 CookieCollection。 |
| GetHashCode() | 作为默认哈希函数。(从 Object 继承。) |
| GetType() | 获取当前实例的 Type。(从 Object 继承。) |
| MemberwiseClone() | 创建当前 Object 的浅表副本。(从 Object 继承。) |
| SetCookies(Uri^, String^) | 将 HTTP Cookie 标头中的一个或多个 Cookie 的 Cookie 实例添加到特定 URI 的 CookieContainer 中。 |
| ToString() | 返回表示当前对象的字符串。(从 Object 继承。) |
字段
名称 | 说明 | |
---|---|---|
| DefaultCookieLengthLimit | 表示 CookieContainer 可以包含的 Cookie 实例的默认最大大小(以字节为单位)。此字段为常数。 |
| DefaultCookieLimit | 表示 CookieContainer 可以包含的 Cookie 实例的默认最大数目。此字段为常数。 |
| DefaultPerDomainCookieLimit | 表示 CookieContainer 可以在每个域引用的 Cookie 实例的默认最大数目。此字段为常数。 |
备注
CookieContainer 是为 Cookie 类的实例提供存储空间的数据结构,该结构以类似于数据库的方式来进行访问。 CookieContainer 具有容量限制,此限制在创建容器或通过属性更改容器时设置。
Cookie 类的实例被添加到基于其起始 URI 的容器。它会被添加到与该 URI 关联的内部 CookieCollection。 Cookie 作为 CookieCollection 或作为可用于提交 HTTP WebRequest 的字符串,从基于该 URI 的容器中进行检索。
CookieContainer 有三项属性用于管理容器的内容容量:Capacity、MaxCookieSize 和 PerDomainCapacity。这些值的默认设置分别为 300、4096 和 20。当一个 Cookie 被添加到容器中时,这些属性将用于确定是否应丢弃已包含在 CookieContainer 中的 Cookie,以便给新的 Cookie 让出空间。 CookieContainer 对每一次添加进行跟踪,以确保不会超过 Capacity 限制或 PerDomainCapacity 限制。如果超过这两项限制或其中一项限制,则将移除 CookieContainer 所包含的 Cookie 实例。首先,所有过期的 Cookie 都将被移除。如果必须进一步收回容量,则清除最早使用的CookieCollection。
转载于:https://www.cnblogs.com/jiaoxh/p/5092135.html
版权声明:本文为CSDN博主「weixin_30682127」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_30682127/article/details/99773856