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 ...
随机推荐
- JD-GUI反编译后代码逻辑分析
一,用jd-gui.exe等工具查看源代码.如何你不会,可以参看此文章: http://blog.csdn.net/hp_2008/article/details/8207879 可以到以下连接下载可 ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- bootstrap基本标签总结[转]
文件头: DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- lua package.path的使用
需要用lua写一个工具,c++调用lua,然后这个lua要require其他lua脚本,在主lua里面设置package.path,但一直都失败,甚至lua都无法编译通过. project_path ...
- git 版本历史
版本:git rev-parse --git-dir显示Git版本库的位置 --show-cdup显示当前工作区目录的深度 --parseopt解析命令行参数 $ git rev-parse - ...
- Sublime Text 教程
编辑器的选择(Editor Choices) 从初学编程到现在,我用过的编辑器有EditPlus.UltraEdit.Notepad++.Vim.TextMate和Sublime Text,如果让我从 ...
- UILabel文本垂直顶部对齐的方法
也不知道为什么UILabel本身没有提供文本垂直顶部对齐的方法,真的有点晕.我们创建一个简单的UILabel来看看: [box type="info"] UILabel *myLa ...
- Oracle数据库字符串连接方法
转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...
- linuxmint获取root
1.进入系统à点击桌面左下角的菜单à点击系统设置 2. 在系统设置里面找到登陆窗口并进去 3.打入自己设置的开机登陆密码 4. 选择选项,并把运行root登陆的勾打上 5.重启生效