惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
H
Heimdal Security Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
S
Securelist
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
博客园 - 叶小钗
J
Java Code Geeks
V
V2EX
博客园 - Franky
Spread Privacy
Spread Privacy
K
Kaspersky official blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
C
CERT Recently Published Vulnerability Notes
Latest news
Latest news
NISL@THU
NISL@THU
罗磊的独立博客
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
V
Visual Studio Blog

博客园 - SAL

【转】WinForm窗体显示和窗体间传值 【转】Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化 常用的几种OCR方法/组件小结(C#) URL重写html后Html文件打不开解决办法 【转】SQL SERVER 2005/2008 中关于架构的理解 Visual Studio、.net framework、CLR与JDK、JRE、JVM、Eclipse 【转】让Entity Framework不再私闯sys.databases 【转】MVC Model建模及Entity Framework Power Tool使用 【转】NuGet学习笔记 【转】一点一点学ASP.NET之基础概念——HttpModule 【转】如何在ASP.NET 2.0中定制Expression Builders 【转】C#微信公众平台开发者模式开启代码 【转】合理的布局,绚丽的样式,谈谈Winform程序的界面设计 【转】winform程序textbox滚动条保持在最下面 内容不闪烁 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。 【转】C#中的委托,匿名方法和Lambda表达式 【转】WCF 服务第一次调用慢的问题 【转】“无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法 【转】开源Word读写组件DocX介绍与入门
【转】HttpWebRequest 保持session
SAL · 2013-08-20 · via 博客园 - SAL

通过HttpWebRequest获取网页内容并保持session,最主要的就是存储cookie。这里使用了一个静态变量m_Cookie用来存储cookie的内容。第二次请求网页的时候把cookie传送过去,这样就可以保持session。

  1. public partial class RequestPage : System.Web.UI.Page  
  2.     {  
  3.         private static CookieContainer m_Cookie = new CookieContainer();  
  4.         private string m_Url = "http://localhost/HttpRequestTest/SessionPage.aspx";  
  5.         protected void Page_Load(object sender, EventArgs e)  
  6.         {  
  7.             string content = GetPageContent();  
  8.               
  9.             Label1.Text = content;  
  10.         }  
  11.   
  12.           
  13.           
  14.           
  15.           
  16.         private string GetPageContent()  
  17.         {  
  18.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_Url);  
  19.             request.CookieContainer = m_Cookie;  
  20.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  21.             string cookieheader = request.CookieContainer.GetCookieHeader(new Uri(m_Url));  
  22.             m_Cookie.SetCookies(new Uri(m_Url), cookieheader);  
  23.   
  24.             Stream stream = response.GetResponseStream();  
  25.             StreamReader reader = new StreamReader(stream);  
  26.             string result = reader.ReadToEnd();  
  27.   
  28.             stream.Close();  
  29.             reader.Close();  
  30.             response.Close();  
  31.   
  32.             return result;  
  33.         }  
  34.   
  35.           
  36.           
  37.           
  38.           
  39.           
  40.         public string GetPageContent(string url)  
  41.         {  
  42.             StringBuilder result = new StringBuilder("");  
  43.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); ;  
  44.             HttpWebResponse reponse = null;  
  45.             try  
  46.             {  
  47.                 request.CookieContainer = m_Cookie;  
  48.                 reponse = (HttpWebResponse)request.GetResponse();  
  49.                 m_Cookie = request.CookieContainer;  
  50.                 Stream rspStream = reponse.GetResponseStream();  
  51.                 StreamReader sr = new StreamReader(rspStream, System.Text.Encoding.Default);  
  52.   
  53.                   
  54.                 Char[] read = new Char[256];  
  55.                 int count = sr.Read(read, 0, 256);  
  56.                 while (count > 0)  
  57.                 {  
  58.                     result.Append(read, 0, count);  
  59.                     count = sr.Read(read, 0, 256);  
  60.                 }  
  61.             }  
  62.             catch (Exception e)  
  63.             {  
  64.                 result.Append(e.Message);  
  65.             }  
  66.             finally  
  67.             {  
  68.                 if (reponse != null)  
  69.                 {  
  70.                     reponse.Close();  
  71.                 }  
  72.             }  
  73.             return result.ToString();  
  74.         }  
  75.     }