HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html]
PostLogin :登录,并保存Cookie

1 public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
2 {
3 ASCIIEncoding encoding = new ASCIIEncoding();
4 byte[] data = encoding.GetBytes(postData);
5 //向服务端请求
6 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
7 myRequest.Method = "POST";
8 myRequest.ContentType = "application/x-www-form-urlencoded";
9 myRequest.ContentLength = data.Length;
10 myRequest.CookieContainer = new CookieContainer();
11 Stream newStream = myRequest.GetRequestStream();
12 newStream.Write(data, 0, data.Length);
13 newStream.Close();
14 //将请求的结果发送给客户端(界面、应用)
15 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
16 cookie.Add(myResponse.Cookies);
17 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
18 return reader.ReadToEnd();
19 }

PostRequest :登录后使用Cookie进行其他操作

1 public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
2 {
3 ASCIIEncoding encoding = new ASCIIEncoding();
4 byte[] data = encoding.GetBytes(postData);
5 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
6 myRequest.Method = "POST";
7 myRequest.ContentType = "application/x-www-form-urlencoded";
8 myRequest.ContentLength = data.Length;
9 myRequest.CookieContainer = cookie;
10 Stream newStream = myRequest.GetRequestStream();
11 newStream.Write(data, 0, data.Length);
12 newStream.Close();
13 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
14 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
15 return reader.ReadToEnd();
16 }

e.g.

1 string strIMSPhone = tb_IMSPhone.Text.Trim();
2 string strIMSPwd = tb_IMSPwd.Text.Trim();
3 string postData = "username=" + strIMSPhone + "&password=" + strIMSPwd + "&type=2";
4 CookieContainer cookie=new CookieContainer();
5 if (IMSHelper.PostLogin(postData, post_signIn, ref cookie).Equals("ok"))
6 {
7 string strCont = PostRequest("", post_getContJsonData, cookie);
8 }

HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用的更多相关文章
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...
- c# 使用 HttpWebRequest模拟登陆
c# 使用 HttpWebRequest模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...
- 使用C#的HttpWebRequest模拟登陆访问人人网
使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路: 第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交的 ...
- 使用C#的HttpWebRequest模拟登陆访问人人网(转)
无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...
- 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)
前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...
- 转:使用C#的HttpWebRequest模拟登陆网站
这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...
- 使用C#的HttpWebRequest模拟登陆网站
很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...
- python利用selenium(webdriver chrome)模拟登陆获取cookie
(我是在windows下进行实验的) 准备工作: 1.安装python环境. 2.python安装selenium插件(执行以下命令就行). pip install selenium 3.Wind ...
随机推荐
- linux内核打印"BUG: scheduling while atomic
linux内核打印"BUG: scheduling while atomic"和"bad: scheduling from the idle thread"错误 ...
- C++ 学习笔记(一)
只是记录自己学习C++ 笔记实例来自<c++ primer> 1.static: static 局部对象确保不迟于在程序执行流程第一次经过该对象的定义语句时进行初始化.这种对象一旦被创建, ...
- windows多线程编程(一)(转)
源出处:http://www.cnblogs.com/TenosDoIt/archive/2013/04/15/3022036.html CreateThread:Windows的API函数(SDK函 ...
- 阿里云无线&前端团队是如何基于webpack实现前端工程化的
背景 前端经历了初期的野蛮生长(切图,写简单的特效)——为了兼容浏览器兼容性而出现的各种类库(JQUERY,YUI等——mv*(饱暖思淫欲,代码多了,也就想到怎样组织代码结构,backbone,ang ...
- 不同平台下Java环境变量的设置
http://www.java.com/en/download/help/path.xml
- 【BZOJ】【2705】【SDOI2012】Longge的问题
欧拉函数/狄利克雷卷积/积性函数 2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1275 Solv ...
- javaScript中eval()方法转换json对象
<script language="javascript"> var user = '{name:"张三",age:23,'+ 'address:{ ...
- 阿里云centos配置ftp和svn全过程
1.下载xshell 2.登录centos 3.安装vsftpd [root@xxx]# yum install vsftpd //安装vsftpd [root@xxx]# chkconfig vsf ...
- JavaScript 语言基础知识点总结(思维导图)
JavaScript 数组 JavaScript 函数基础 Javascript 运算符 JavaScript 流程控制 JavaScript 正则表达式 JavaScript 字符串函数 JavaS ...
- linux - 使用curl实现新浪天气API应用
新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...