【转】HttpWebRequest 保持session
通过HttpWebRequest获取网页内容并保持session,最主要的就是存储cookie。这里使用了一个静态变量m_Cookie用来存储cookie的内容。第二次请求网页的时候把cookie传送过去,这样就可以保持session。
- public partial class RequestPage : System.Web.UI.Page
- {
- private static CookieContainer m_Cookie = new CookieContainer();
- private string m_Url = "http://localhost/HttpRequestTest/SessionPage.aspx";
- protected void Page_Load(object sender, EventArgs e)
- {
- string content = GetPageContent();
- //string content = GetPageContent(m_Url);
- Label1.Text = content;
- }
- /// <summary>
- /// 获取页面内容,保存CookieHeader
- /// </summary>
- /// <returns></returns>
- private string GetPageContent()
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_Url);
- request.CookieContainer = m_Cookie;
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- string cookieheader = request.CookieContainer.GetCookieHeader(new Uri(m_Url));
- m_Cookie.SetCookies(new Uri(m_Url), cookieheader);
- Stream stream = response.GetResponseStream();
- StreamReader reader = new StreamReader(stream);
- string result = reader.ReadToEnd();
- stream.Close();
- reader.Close();
- response.Close();
- return result;
- }
- /// <summary>
- /// 获取页面内容,存储CookieContainer
- /// </summary>
- /// <param name="url">被请求页面的url</param>
- /// <returns>返回页面内容</returns>
- public string GetPageContent(string url)
- {
- StringBuilder result = new StringBuilder("");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); ;
- HttpWebResponse reponse = null;
- try
- {
- request.CookieContainer = m_Cookie;
- reponse = (HttpWebResponse)request.GetResponse();
- m_Cookie = request.CookieContainer;
- Stream rspStream = reponse.GetResponseStream();
- StreamReader sr = new StreamReader(rspStream, System.Text.Encoding.Default);
- //获取数据
- Char[] read = new Char[256];
- int count = sr.Read(read, 0, 256);
- while (count > 0)
- {
- result.Append(read, 0, count);
- count = sr.Read(read, 0, 256);
- }
- }
- catch (Exception e)
- {
- result.Append(e.Message);
- }
- finally
- {
- if (reponse != null)
- {
- reponse.Close();
- }
- }
- return result.ToString();
- }
- }
【转】HttpWebRequest 保持session的更多相关文章
- HttpWebRequest 保存Cookies,模拟Session登录
前面使用HttpWebRequest 对象可以抓取网页中一些资料,不过有些页面可以直接打开,而有些页面必登录之后才能打开,也就是在登录后保存登录信息在Session,这样就可以访问有权限的页面了.下面 ...
- HttpWebRequest调用WebService后台需要Session信息问题的解决办法
今天在用HttpWebRequest调用后台ASP.NET 的WebService方法时遇到了一个问题,后台的WebService方法里使用到了Session对象中的用户信息,而Session对象中的 ...
- web也是区分前端与后端的,session\cookie辨析
<1>Ajax交互方式 Ext.Ajax.request( { //被用来向服务器发起请求默认的url url : "", //请求时发送后台的参数,既可以是Json对 ...
- 通过HttpWebRequest请求与HttpWebResponse响应方式发布接口与访问接口
一.API接口的编码 1.首页的页面代码: protected void Page_Load(object sender, EventArgs e) { /* * 请求路径:http://xxxx/i ...
- C#的提交表单方式主要有两种WebClient与HttpWebRequest
根据黄聪:C#模拟网站页面POST数据提交表单(转) using System; using System.Collections.Generic; using System.IO; using Sy ...
- C#获得和发送网站Session
request = (HttpWebRequest)WebRequest.Create(url); if (Const. ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie
之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...
- C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...
- WebApi 能支持Session
由于项目实际需要,我希望让WebApi服务也能支持Session,所以便查找资料按照网上的方法开始着手实验. 然后就有了以下的代码,主要是说让WebApi支持Session,要重写Global.asa ...
随机推荐
- [HTTP]Nonocast.http post方法
Nonocast.Http is a free, open source developer focused web service via http for small and medium sof ...
- unity面试准备
最近有换工作的打算 所以上网看下面试题 自己做下总结 Q:ArrayList 和 List区别 A: 1:List大家都知道初始化的时候需要定义其类型,例如 List<int> listT ...
- 使用bmfont制作字体
本地显示正常 将制作好的字体上传 别人用不好使 制作完场景没ctrl+s 保存 ctrl+s保存之后生成另外的文件
- 方法引用(Method reference)和invokedynamic指令详细分析
方法引用(Method reference)和invokedynamic指令详细分析 invokedynamic是jvm指令集里面最复杂的一条.本文将详细分析invokedynamic指令是如何实现方 ...
- CRC32算法笔记
这几天在研究CRC32的计算过程,看了CRC算法的原理,也看了不少通过移位法实现的代码,但是算出的结果跟校验工具算的不一致. 折腾了好长时间,终于找到一个手工计算CRC32的文章,再对照IEEE 80 ...
- css 清楚浮动三种方法
我们可以看到这样一个布局: <style> .left{ width: 200px; height: 200px; background-color: #00ee00; float: le ...
- 启动MacOS 本地服务
MacOS 自带Apatch服务器, 在浏览器输入 http://127.0.0.1/ 出现it works,代表访问成功 一. 启动 启动 sudo apachectl start 重启 sudo ...
- 进阶篇:4.4)DFA设计指南:面向高速自动化装配设计
本章目标:更进一步,设计出符合高速自动化装配的零件. 1.前言 中国的人口红利时代正在慢慢地过去,这是事实.同时,机器换人与大自动化的时代也在到来. 在这个时代中,人工成本越来越高,零部件的装配和库存 ...
- P1001 A+B Problem (树链剖分)
这题考验我们构造模型的能力. 考虑构造一棵树,树上有3个节点,节点1和节点2连一条权值为a的边,节点1和节点3连一条权值为b的边,显然答案就是节点2到节点3的最短路径. 但这样还不够.考虑加法的性质, ...
- Android调用 .Net Core WebApi 返回数据,用FastJSON解析一直报错。
问题描述:.Net Core WebApi中用Newtonsoft.Json 把datatable转成json字符串,如:JsonConvert.SerializeObject(table,Forma ...