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

推荐订阅源

爱范儿
爱范儿
T
Troy Hunt's Blog
B
Blog
N
Netflix TechBlog - Medium
H
Help Net Security
PCI Perspectives
PCI Perspectives
罗磊的独立博客
SecWiki News
SecWiki News
S
Security Affairs
Webroot Blog
Webroot Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
博客园 - 【当耐特】
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
Hugging Face - Blog
Hugging Face - Blog
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
MIT News - Artificial intelligence
博客园_首页
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
The Cloudflare Blog
P
Proofpoint News Feed
D
DataBreaches.Net
N
News and Events Feed by Topic
G
Google Developers Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AI
AI
O
OpenAI News
雷峰网
雷峰网
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队

博客园 - 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.     }