CookieContainer

HttpWebRequest属性提供CookieContainer类的一个实例,该实例包含与此请求关联的 cookie。

 1         CookieContainer c = new CookieContainer();    //声明一个CookieContainer

 2         HttpWebRequest requset = (HttpWebRequest)WebRequest.Create("URL1登录页");

 3         requset.CookieContainer = c;      //登录相关的Cookie

 4         HttpWebResponse respons = (HttpWebResponse)requset.GetResponse();

 5         

 6         Stream dataStream = respons.GetResponseStream();

 7         StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("gb2312"));

 8         string s = reader.ReadToEnd();

 9         dataStream.Close();

10         reader.Close();

11 

12         requset = (HttpWebRequest)WebRequest.Create("URL2其他页面");

13         requset.CookieContainer = c;     //呆着登录相关的Cookie去访问内部页面

14         respons = (HttpWebResponse)requset.GetResponse();

15 

16         dataStream = respons.GetResponseStream();

17         reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("gb2312"));

18         s = reader.ReadToEnd();

19         dataStream.Close();

20         reader.Close();

21         Response.Write(s);  

你可能感兴趣的:(contain)