我要用多个账号密码登录一个网站。然后做其他操作。
问题是怎么吧COOKIES 保存起来,然后要用哪个账号在调出COOKIES现在我是直接封装的一个类然后 NEW 一个对象一直不释放,但很占用资源现在我查质料看见有两种方法保存但差别非常大
一个是 HttpWebResponse 中保存。
另一个是HttpWebRequest 中保存。我想问到底COOKIES 保证到哪儿的。从哪儿能取出来啦??? CookieContainer cc = new CookieContainer();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cc;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET"; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding); if (httpWebResponse.Cookies.Count > 0)
{
cc.Add(httpWebResponse.Cookies);
} string html = streamReader.ReadToEnd();
CookieContainer cookies = new CookieContainer();
string url = "http://www.google.com.hk/";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例
myHttpWebRequest.GetResponse().Close();
cookies = myHttpWebRequest.CookieContainer; //保存cookies
string cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串 url = "http://www.google.com.hk/search?oe=utf8&ie=utf8&source=uds&hl=zh-CN&q=3g";
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
myHttpWebRequest.CookieContainer = cookies; //使用已经保存的cookies 方法一
//myHttpWebRequest.Headers.Add("Cookie", cookiesstr); //使用已经保存的cookies 方法二
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream stream = myHttpWebResponse.GetResponseStream();
stream.ReadTimeout = 15 * 1000; //读取超时
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
string strWebData = sr.ReadToEnd();
问题是怎么吧COOKIES 保存起来,然后要用哪个账号在调出COOKIES现在我是直接封装的一个类然后 NEW 一个对象一直不释放,但很占用资源现在我查质料看见有两种方法保存但差别非常大
一个是 HttpWebResponse 中保存。
另一个是HttpWebRequest 中保存。我想问到底COOKIES 保证到哪儿的。从哪儿能取出来啦??? CookieContainer cc = new CookieContainer();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cc;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET"; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding); if (httpWebResponse.Cookies.Count > 0)
{
cc.Add(httpWebResponse.Cookies);
} string html = streamReader.ReadToEnd();
CookieContainer cookies = new CookieContainer();
string url = "http://www.google.com.hk/";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例
myHttpWebRequest.GetResponse().Close();
cookies = myHttpWebRequest.CookieContainer; //保存cookies
string cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串 url = "http://www.google.com.hk/search?oe=utf8&ie=utf8&source=uds&hl=zh-CN&q=3g";
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
myHttpWebRequest.CookieContainer = cookies; //使用已经保存的cookies 方法一
//myHttpWebRequest.Headers.Add("Cookie", cookiesstr); //使用已经保存的cookies 方法二
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream stream = myHttpWebResponse.GetResponseStream();
stream.ReadTimeout = 15 * 1000; //读取超时
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
string strWebData = sr.ReadToEnd();
就是用cookie存账号密码咯
吧多个账号分开存每个账号一个COOKIES 然后 要用哪个账号就调用那个COOKIES