[一篮饭特稀原创,转载请注明出自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以便登录请求后使用的更多相关文章

  1. C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用

    public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...

  2. c# 使用 HttpWebRequest模拟登陆

    c# 使用 HttpWebRequest模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...

  3. c# 使用 HttpWebRequest模拟登陆(附带验证码)

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...

  4. 使用C#的HttpWebRequest模拟登陆访问人人网

    使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路: 第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交的 ...

  5. 使用C#的HttpWebRequest模拟登陆访问人人网(转)

    无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...

  6. 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)

    前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...

  7. 转:使用C#的HttpWebRequest模拟登陆网站

    这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...

  8. 使用C#的HttpWebRequest模拟登陆网站

    很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...

  9. python利用selenium(webdriver chrome)模拟登陆获取cookie

    (我是在windows下进行实验的) 准备工作: 1.安装python环境. 2.python安装selenium插件(执行以下命令就行).   pip install selenium 3.Wind ...

随机推荐

  1. 【ExtJs】使用Cookie、切换主题和语言

    转自:http://witmax.cn/extjs-cookie-theme-lang.html 使用Cookie:   1 2 3 Ext.state.Manager.setProvider(new ...

  2. 如何配置DNS服务器(局域网——域名指向某个IP地址)

    单击“开始”,指向“管理工具”,然后单击“DNS”,打开 DNS 管理器.   如有必要,向管理单元添加适用的服务器,然后连接该服务器.在控制台树中,单击适用的 DNS 服务器.   在“操作”菜单上 ...

  3. MYSQL 一些用法

    LOAD DATA LOCAL INFILE 'C:/xampp/htdocs/test/file/sample.csv' INTO TABLE sample1 FIELDS TERMINATED B ...

  4. 最基础的Hash

    type thash=^node; node=record state:longint; next:thash; end; var a,i:longint; p:thash; hash:..]of t ...

  5. spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描

    1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK

  6. 20145103JAVA第一次实验报告

    20145103<Java程序设计>第一次实验报告 实验内容及其步骤 一.命令行下java程序开发 建立一个java文件,然后在命令行中,对程序进行javac编译,就生成了.class文件 ...

  7. 【 Regular Expression Matching 】cpp

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  8. Javascript使用function创建类的两种方法

    1.使用function类 //myFunction.js var CMyFunc=function() { //类的公共方法,供外部调用 this.Func1=function() { var i= ...

  9. 【bzoj1013】[JSOI2008]球形空间产生器sphere

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4530  Solved: 2364[Subm ...

  10. CSS中常用的字体单位:px、em、rem和%的区别

    在刚接触CSS时,px用的比较多,也很好理解,可是用久了就会发现有些缺陷,特别是在做响应式开发的时候. 那这么多单位到底在什么时候用什么单位合适呢?今天就来探讨一下. 先大致解释一下这些单位的意思: ...