这两天做了一个获取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; } }
|
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
|
译文
原文
|
CookieCollection 对象的集合提供容器。
程序集: System(System.dll 中)
System.Net::CookieContainer
| 名称 | 说明 | |
|---|---|---|
| CookieContainer() |
CookieContainer 类的新实例。 |
|
| CookieContainer(Int32) |
CookieContainer 类的新实例。 |
|
| CookieContainer(Int32, Int32, Int32) |
CookieContainer 类的新实例。 |
| 名称 | 说明 | |
|---|---|---|
| Capacity |
Cookie 实例数。 |
|
| Count |
Cookie 实例数。 |
|
| MaxCookieSize |
Cookie 的最大允许长度。 |
|
| PerDomainCapacity |
Cookie 实例数。 |
| 名称 | 说明 | |
|---|---|---|
| Add(Cookie^) |
Cookie 与哪个域集合相关联。 |
|
| Add(CookieCollection^) |
CookieContainer 中。 |
|
| Add(Uri^, Cookie^) |
CookieContainer 中。 |
|
| Add(Uri^, CookieCollection^) |
CookieContainer 中。 |
|
| Equals(Object^) |
Object 继承。) |
|
| Finalize() |
Object 继承。) |
|
| GetCookieHeader(Uri^) |
Cookie 实例的 HTTP Cookie。 |
|
| GetCookies(Uri^) |
CookieCollection。 |
|
| GetHashCode() |
Object 继承。) |
|
| GetType() |
Object 继承。) |
|
| MemberwiseClone() |
Object 继承。) |
|
| SetCookies(Uri^, String^) |
CookieContainer 中。 |
|
| ToString() |
Object 继承。) |
| 名称 | 说明 | |
|---|---|---|
|
|
DefaultCookieLengthLimit |
此字段为常数。 |
|
|
DefaultCookieLimit |
此字段为常数。 |
|
|
DefaultPerDomainCookieLimit |
此字段为常数。 |
CookieContainer 具有容量限制,此限制在创建容器或通过属性更改容器时设置。
CookieCollection 或作为可用于提交 HTTP WebRequest 的字符串,从基于该 URI 的容器中进行检索。
CookieCollection。