本站业务范围:1、PC端软件开发、网站开发 2、移动端APP、网站、微信接口、微商城开发 3、视频教程、毕业设计辅导
 当前位置:文章中心 >> Winform/客户端程序
立即购买视频教程 模拟登录人人网
夜鹰教程网 来源:www.yyjcw.com 日期:2017/1/8 22:45:41
分享到:
模拟登录人人网

这篇文章不能解决你的问题?我们还有相关视频教程哦!

QQ:1416759661 新手QQ群:7835003  加Q远程协助!

在线解答|Demo制作|远程调试| 点击查看相关的视频教程

技术咨询: 购买教程  技术咨询:购买教程  大胆的加QQ咨询。

 这篇文章是有关模拟登录网站方面的。

实现步骤;

    启用一个web会话
    发送模拟数据请求(POST或者GET)
    获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据。

我们以登录人人网为例,首先需要分析人人网登录时POST的数据格式,这个可以通过IE9中只带的F12快捷键,调出开发人员工具。如下图:

 

通过开始捕获得到POST的地址和POST的数据

POST数据:

email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476

POST地址:

http://www.renren.com/PLogin.do

下面就是代码示例来得到登录后页面(http://guide.renren.com/guide)的数据

HTMLHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Test
{
   public class HTMLHelper
    {
       /// <summary>
        /// 获取CooKie
       /// </summary>
       /// <param name="loginUrl"></param>
       /// <param name="postdata"></param>
       /// <param name="header"></param>
       /// <returns></returns>
       public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
       {
           HttpWebRequest request = null;
           HttpWebResponse response = null;
           try
           {
               CookieContainer cc = new CookieContainer();
               request = (HttpWebRequest)WebRequest.Create(loginUrl);
               request.Method = header.method;
               request.ContentType = header.contentType;
               byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
               request.ContentLength = postdatabyte.Length;
               request.AllowAutoRedirect = false;
               request.CookieContainer = cc;
               request.KeepAlive = true;

               //提交请求
               Stream stream;
               stream = request.GetRequestStream();
               stream.Write(postdatabyte, 0, postdatabyte.Length);
               stream.Close();

               //接收响应
               response = (HttpWebResponse)request.GetResponse();
               response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

               CookieCollection cook = response.Cookies;
               //Cookie字符串格式
               string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

               return cc;
           }
           catch (Exception ex)
           {
               
               throw ex;
           }
       }

       /// <summary>
       /// 获取html
       /// </summary>
       /// <param name="getUrl"></param>
       /// <param name="cookieContainer"></param>
       /// <param name="header"></param>
       /// <returns></returns>
       public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
       {
           Thread.Sleep(1000);
           HttpWebRequest httpWebRequest = null;
           HttpWebResponse httpWebResponse = null;
           try
           {
               httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
               httpWebRequest.CookieContainer = cookieContainer;
               httpWebRequest.ContentType = header.contentType;
               httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
               httpWebRequest.Referer = getUrl;
               httpWebRequest.Accept = header.accept;
               httpWebRequest.UserAgent = header.userAgent;
               httpWebRequest.Method = "GET";
               httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
               Stream responseStream = httpWebResponse.GetResponseStream();
               StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
               string html = streamReader.ReadToEnd();
               streamReader.Close();
               responseStream.Close();
               httpWebRequest.Abort();
               httpWebResponse.Close();
               return html;
           }
           catch (Exception e)
           {
               if (httpWebRequest != null) httpWebRequest.Abort();
               if (httpWebResponse != null) httpWebResponse.Close();
               return string.Empty;
           }
       }
    }

   public class HttpHeader
   {
       public string contentType { get; set; }

       public string accept { get; set; }

       public string userAgent { get; set; }

       public string method{get;set;}

       public int maxTry { get; set; }
   }
}

 

测试用例:

  HttpHeader header = new HttpHeader();
                header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
                header.contentType = "application/x-www-form-urlencoded";
                header.method = "POST";
                header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                header.maxTry = 300;

                string html = HTMLHelper.GetHtml("http://guide.renren.com/guide", HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",
                    "email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header), header);

                Console.WriteLine(html);


               Console.ReadLine();

 

通过程序登录了网站后而直接进入登录后的页面。

首先还是发起一个启用一个web会话,然后发送模拟数据请求,获取会话的CooKie,再根据该CooKie将其写入到本地,通过程序直接打开登录后的页面。

该功能可用于无法修改第三方系统源代码而要做系统单点登录。

 

我们先在HTMLHelper类中添加一个方法:

 1 /// <summary>
 2 /// 获取CookieCollection
 3 /// </summary>
 4 /// <param name="loginUrl"></param>
 5 /// <param name="postdata"></param>
 6 /// <param name="header"></param>
 7 /// <returns></returns>
 8        public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)
 9        {
10            HttpWebRequest request = null;
11            HttpWebResponse response = null;
12            try
13            {
14                CookieContainer cc = new CookieContainer();
15                request = (HttpWebRequest)WebRequest.Create(loginUrl);
16                request.Method = header.method;
17                request.ContentType = header.contentType;
18                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
19                request.ContentLength = postdatabyte.Length;
20                request.AllowAutoRedirect = false;
21                request.CookieContainer = cc;
22                request.KeepAlive = true;
23
24                //提交请求
25                Stream stream;
26                stream = request.GetRequestStream();
27                stream.Write(postdatabyte, 0, postdatabyte.Length);
28                stream.Close();
29
30                //接收响应
31                response = (HttpWebResponse)request.GetResponse();
32                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
33
34                CookieCollection cook = response.Cookies;
35                //Cookie字符串格式
36                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
37
38                return cook;
39            }
40            catch (Exception ex)
41            {
42
43                throw ex;
44            }
45        }


再根据获取的CookieCollection写入本地,并打开登录后的页面

 1   [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
 2
 3         public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
 4
 5
 6   HttpHeader header = new HttpHeader();
 7                 header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
 8                 header.contentType = "application/x-www-form-urlencoded";
 9                 header.method = "POST";
10                 header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
11                 header.maxTry = 300;
12
13
14  CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",
15                     "email=aaa%40163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header);
16
17
18  foreach (Cookie cookie in mycookie) //将cookie设置为浏览的cookie  
19                 {
20
21                     InternetSetCookie(
22
23                          "http://" + cookie.Domain.ToString(),
24
25                          cookie.Name.ToString(),
26
27                          cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");
28
29                 }
30                 System.Diagnostics.Process.Start("http://guide.renren.com/guide");

复制代码

这样即可直接通过程序打开登录后的页面:

 

复制链接 网友评论 收藏本文 关闭此页
上一条: .NET C# 和Java开发人员岗位切换  下一条: HttpWebRequest以及HttpWebResponse介绍…
购买夜鹰教程网视频教程
  推荐教程/优惠活动

  热门服务/教程目录

  夜鹰教程网  新手必看,详细又全面。
  夜鹰教程网  购买教程  夜鹰教程网  在线支付-方便
  夜鹰教程网  担保交易-快捷安全   夜鹰教程网  闪电发货
  夜鹰教程网  电话和QQ随时可以联系我们。
  夜鹰教程网 不会的功能都可以找我们,按工作量收费。

客服电话:153 9760 0032

购买教程QQ:1416759661  购买教程
  热点推荐
破解封路由的方法详解
技巧篇:只用这一招就能拒绝全部的…
挂马方法和技巧大汇总
网络安全防护:如何防止远程入侵
如何解决:系统开机就弹出广告网页…
彻底防御U盘病毒
安全配置交换机端口提高网络安全性…
浅析网络安全审计原理和技术(2)
用U盘就可以去除win XP管理员密码…
电脑硬盘各逻辑盘用鼠标左键双击它…
详解网站挂马的手段
新手认识;网银账号相关知识
ARP欺骗和ICMP欺骗催生全新DOS工具…
浅析网络安全审计原理和技术(1)
木马最基本的隐藏:不可见窗体和隐…
  尊贵服务
夜鹰教程网 承接业务:软件开发 网站开发 网页设计 .Net+C#+VS2008+MSsql+Jquery+ExtJs全套高清完整版视频教程
  最近更新
.NET C# 和Java开发人员岗位切换
模拟登录人人网
HttpWebRequest以及HttpWebRespon…
HttpClient使用说明
Winform为Combobox树添加数据
C#之允许DataGridView选中整行
C#中DataGridView只能选择一行禁止…
为自动填充列调整大小期间不能执行…
c# winform设置listBox如何设置te…
c#的委托(代理)和事件
C# Task 任务计划
C# socket 多线程多管道可断点传送…
让ListBox的滚动条滚动到最底部
c#用事件模式实现通知
什么是异步编程,为什么会需要异…
  工具下载  需要远程协助? 购买教程

sql2008视频教程 c#视频教程

VIP服务:如果您的某个功能不会做,可以加我们QQ,给你做DEMO!

JQUERY  Asp.net教程

MVC视频教程  vs2012
.NET+sql开发
手机:15397600032 C#视频教程下载
vs2012 vs2008 vs2010 sql2008 sql2005
Asp.Net  

教程咨询QQ:1416759661


这篇文章不能解决你的问题?我们还有相关视频教程哦!

QQ:1416759661 新手QQ群:7835003  加Q远程协助!

在线解答|Demo制作|远程调试| 点击查看相关的视频教程

技术咨询: 购买教程  技术咨询:购买教程  大胆的加QQ咨询。

关于我们 | 广告投放 | 购买教程 | 网站建设 | 技术辅导 | 常见问题 | 联系我们 | 友情链接 | 购买教程

Nighthawk Tutorials Network 快捷购买教程

夜鹰教程网 版权所有 www.yyjcw.com All rights reserved 备案号:蜀ICP备08011740号3

 

出售asp.net系列视频教程 高清原创版 每天晚上20:30--23:00在线 按照编号从简单到高级 http://www.yyjcw.com/list.html