c# winform实现网页上用户自动登陆,模拟网站登录
using System;
using System.Collections.Generic; using System.Text; using System.Net; using System.IO;namespace Czt.Web { /// <summary> /// 实现网站登录类 /// </summary> public class Post { /// <summary> /// 网站Cookies /// </summary> private string _cookieHeader = string.Empty; public string CookieHeader { get { return _cookieHeader; } set { _cookieHeader = value; } } /// <summary> /// 网站编码 /// </summary> private string _code = string.Empty; public string Code { get { return _code; } set { _code = value; } } private string _pageContent = string.Empty; public string PageContent { get { return _pageContent; } set { _pageContent = value; } } private Dictionary<string, string> _para = new Dictionary<string, string>(); public Dictionary<string, string> Para { get { return _para; } set { _para = value; } } /**/ /// <summary> /// 功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie /// </summary> /// <param name="strURL">登录数据提交的页面地址</param> /// <param name="strArgs">用户登录数据</param> /// <param name="strReferer">引用地址</param> /// <param name="code">网站编码</param> /// <returns>可以返回页面内容或不返回</returns> public string PostData(string strURL, string strArgs, string strReferer, string code, string method) { return PostData(strURL, strArgs, strReferer, code, method, string.Empty); } public string PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType) { try { string strResult = ""; HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); myHttpWebRequest.AllowAutoRedirect = true; myHttpWebRequest.KeepAlive = true; myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*"; myHttpWebRequest.Referer = strReferer; myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)"; if (string.IsNullOrEmpty(contentType)) { myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; } else { myHttpWebRequest.ContentType = "contentType"; } myHttpWebRequest.Method = method; myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); if (myHttpWebRequest.CookieContainer == null) { myHttpWebRequest.CookieContainer = new CookieContainer(); } if (this.CookieHeader.Length > 0) { myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader); myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader); } byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs); myHttpWebRequest.ContentLength = postData.Length; System.IO.Stream PostStream = myHttpWebRequest.GetRequestStream(); PostStream.Write(postData, 0, postData.Length); PostStream.Close(); HttpWebResponse response = null; System.IO.StreamReader sr = null; response = (HttpWebResponse)myHttpWebRequest.GetResponse(); if (myHttpWebRequest.CookieContainer != null) { this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL)); } sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code)); // //utf-8 strResult = sr.ReadToEnd(); sr.Close(); response.Close(); return strResult; } catch (Exception ex) { Utilities.Document.Create("C:\\error.log", strArgs, true, Encoding.UTF8); } return string.Empty; } /**/ /// <summary> /// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容 /// </summary> /// <param name="strURL">获取网站的某页面的地址</param> /// <param name="strReferer">引用的地址</param> /// <returns>返回页面内容</returns> public string GetPage(string strURL, string strReferer, string code) { return GetPage(strURL, strReferer,code,string.Empty); } public string GetPage(string strURL, string strReferer,string code,string contentType) { string strResult = ""; HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); myHttpWebRequest.AllowAutoRedirect = true; myHttpWebRequest.KeepAlive = false; myHttpWebRequest.Accept = "*/*"; myHttpWebRequest.Referer = strReferer; myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)"; if (string.IsNullOrEmpty(contentType)) { myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; } else { myHttpWebRequest.ContentType = contentType; } myHttpWebRequest.Method = "GET"; if (myHttpWebRequest.CookieContainer == null) { myHttpWebRequest.CookieContainer = new CookieContainer(); } if (this.CookieHeader.Length > 0) { myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader); myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader); } HttpWebResponse response = null; System.IO.StreamReader sr = null; response = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream streamReceive; string gzip = response.ContentEncoding; if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip") { streamReceive = response.GetResponseStream(); } else { streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); } sr = new System.IO.StreamReader(streamReceive, Encoding.GetEncoding(code)); if (response.ContentLength > 1) { strResult = sr.ReadToEnd(); } else { char[] buffer=new char[256]; int count = 0; StringBuilder sb = new StringBuilder(); while ((count = sr.Read(buffer, 0, buffer.Length)) > 0) { sb.Append(new string(buffer)); } strResult = sb.ToString(); } sr.Close(); response.Close(); return strResult; } } } c# winform实现网页上用户自动登陆,模拟网站登录的更多相关文章
- Fedora以root用户自动登陆
目录 Fedora以root用户自动登陆 参考 配置自动登陆 Fedora以root用户自动登陆 Fedora Workstation Version: 31
- 让Ecshop网店系统用户自动登陆
让Ecshop网店系统用户户自动登陆,打开ecshop includes/init.php文件,可以发现Ecshop系统判断用户的SESSION不存在的时候会去读取存储在COOKIES里面的值.如下代 ...
- HTML5的audio在手机网页上无法自动加载/播放音乐,能否实现该功能?
在IOS中第一次调用play方法播放音频会被阻止,必须得等用户有交互动作,比如touchstart,click后才能正常调用,在微信中可以通过监听WeixinJSBridgeReady事件来提前播放一 ...
- WINDOWS 2008 SERVER域用户自动登陆
The user I wanted to auto-logon as didn’t have a password, this reg hack worked instead: HKEY_LOCAL_ ...
- Django如何让未登录的用户自动跳转至登录页
有多种方法可以实现: 使用Django自带的用户认证 from django.contrib.auth.decorators import login_required @login_required ...
- Win7多用户情况下,指定某一用户为自动登陆-解决办法
转自:http://sbiuggypm.themex.net/archives/605 许久没更新博客了,但从后台可以查看到,有不少朋友还是几乎每天来逛一逛,很对不起的是最近都没更新啥内容.真是不好意 ...
- C#中模拟用户登陆SharePoint网站
自动化测试一个SharePoint网站,首先要登陆,我们今天就模拟一下用户登陆SharePoint网站的过程,这一过程可以通过其他方式完成模拟,比如通过Coded UI Test录制脚本会更方便,但是 ...
- curl模拟自动登陆&采集网页数据
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- Windows上安装配置SSH教程(5)——win10下使用Cygwin+Expect自动登陆ssh
1.安装Cygwin,安装上Tcl和Expect两个工具. 可以使用apt-cyg命令安装,也可以在安装Cygwin的时候选中这两个包. 命令安装的话使用下面的两个命令: apt-cyg instal ...
随机推荐
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...
- MEF学习小结 z
1.什么是MEF. MEF,全称是Managed Extensibility Framework.它是.NET Framework4.0的一个类库,其主要目的是为了创建可扩展的应用程序.按照官方说法就 ...
- BZOJ2005: [Noi2010]能量采集 莫比乌斯反演的另一种方法——nlogn筛
分析:http://www.cnblogs.com/huhuuu/archive/2011/11/25/2263803.html 注:从这个题收获了两点 1,第一象限(x,y)到(0,0)的线段上整点 ...
- 无法连接 mysql
==================================================================================================== ...
- Fibonacci数列小程序
Fibonacci数列小程序 问题分析:Fibonacci数列特征是前两项数均为1,从第三项起,前两项的和为第三项的数的数值用公式归纳起来为:f1=f2=1.f1=f1+f2.f2=f1+f2. 程序 ...
- 问题-delphi 程序在某电脑中显示???问号 乱码
问题现象:delphi 程序在某电脑中显示???问号 乱码 问题原因:因为语言的原因.不同的国家可能显示的编码不一样. 问题处理:“控制面板”>“区域和语言选项”>“区域选项”>“标 ...
- zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...
- [网络]让局域网的电脑通过路由的公网ip可以被访问到的设置
http://公网IP:服务端口/访问路径(http://110.16.166.135:8081/pra/unionpay/v_asyn_notify.jspx这个地址就会通过公网ip来访问到你的ip ...
- 微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)
转自 http://www.iruanmi.com/visual-studio-2013/ 微软已经向MSDN订阅用户提供了Visual Studio 2013正式版镜像下载,只是非MSDN用户能够在 ...
- C++ Primer--虚函数与纯虚函数的区别
首先:强调一个概念 定义一个函数为虚函数,不代表函数为不被实现的函数. 定义他为虚函数是为了允许用基类的指针来调用子类的这个函数. 定义一个函数为纯虚函数,才代表函数没有被实现. 定义纯虚函数是为了实 ...