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实现网页上用户自动登陆,模拟网站登录的更多相关文章

  1. Fedora以root用户自动登陆

    目录 Fedora以root用户自动登陆 参考 配置自动登陆 Fedora以root用户自动登陆 Fedora Workstation Version: 31

  2. 让Ecshop网店系统用户自动登陆

    让Ecshop网店系统用户户自动登陆,打开ecshop includes/init.php文件,可以发现Ecshop系统判断用户的SESSION不存在的时候会去读取存储在COOKIES里面的值.如下代 ...

  3. HTML5的audio在手机网页上无法自动加载/播放音乐,能否实现该功能?

    在IOS中第一次调用play方法播放音频会被阻止,必须得等用户有交互动作,比如touchstart,click后才能正常调用,在微信中可以通过监听WeixinJSBridgeReady事件来提前播放一 ...

  4. WINDOWS 2008 SERVER域用户自动登陆

    The user I wanted to auto-logon as didn’t have a password, this reg hack worked instead: HKEY_LOCAL_ ...

  5. Django如何让未登录的用户自动跳转至登录页

    有多种方法可以实现: 使用Django自带的用户认证 from django.contrib.auth.decorators import login_required @login_required ...

  6. Win7多用户情况下,指定某一用户为自动登陆-解决办法

    转自:http://sbiuggypm.themex.net/archives/605 许久没更新博客了,但从后台可以查看到,有不少朋友还是几乎每天来逛一逛,很对不起的是最近都没更新啥内容.真是不好意 ...

  7. C#中模拟用户登陆SharePoint网站

    自动化测试一个SharePoint网站,首先要登陆,我们今天就模拟一下用户登陆SharePoint网站的过程,这一过程可以通过其他方式完成模拟,比如通过Coded UI Test录制脚本会更方便,但是 ...

  8. curl模拟自动登陆&采集网页数据

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  9. Windows上安装配置SSH教程(5)——win10下使用Cygwin+Expect自动登陆ssh

    1.安装Cygwin,安装上Tcl和Expect两个工具. 可以使用apt-cyg命令安装,也可以在安装Cygwin的时候选中这两个包. 命令安装的话使用下面的两个命令: apt-cyg instal ...

随机推荐

  1. java CopyOnWriteArrayList的使用

    除了加锁外,其实还有一种方式可以防止并发修改异常,这就是将读写分离技术(不是数据库上的). 先回顾一下一个常识: 1.JAVA中“=”操作只是将引用和某个对象关联,假如同时有一个线程将引用指向另外一个 ...

  2. yarn环境的搭建

    1.首先,在zookeeper搭建成功,服务运行的基础上搭建yarn,其次,保证时间一致 2.在 /home/install/hadoop-2.5/etc/hadoop目录下配置一下几个配置文件: 第 ...

  3. 理解iPhone高清分辨率的问题

    理解iPhone高清分辨率的问题可以有两个关键切入点: 1.像素坐标普通屏是480*320,Retina屏是960*640,而逻辑坐标系同为480*320: 2.Retina屏幕一个逻辑坐标点坐标包含 ...

  4. NOIP2015 提高组(senior) 解题报告

    过了这么久才来发解题报告,蒟蒻实在惭愧 /w\ Day1 T1 [思路] 模拟 [代码] #include<iostream> #include<cstring> #inclu ...

  5. HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5568822.html 第一次遇到迷宫搜索,给我的感觉是十分惊喜的:搞懂这个的话,感觉自己又掌握了一项技能~ 个人 ...

  6. static与线程安全 -摘自网络

    在.Net中,Static会经常和线程的东西扯在一起.写的代码是不是线程安全呢?好多程序员都在想,不过,有时候随便就放过了.真正出问题的时候再想.其实,如果程序员一开始就明白这里面的机制,也许,编写的 ...

  7. Android文字的阴影效果

    <!-- android:shadowDx 阴影的水平偏移量 即往右移的距离 --> <!-- android:shadowDy 阴影的垂直偏移量 即往下移的距离--> < ...

  8. Spring源码入门——DefaultBeanNameGenerator解析

    我们知道在spring中每个bean都要有一个id或者name标示每个唯一的bean,在xml中定义一个bean可以指定其id和name值,但那些没有指定的,或者注解的spring的beanname怎 ...

  9. XML与XHTML

    什么是XML XML的基本格式 XML的定义文档 HTML5的文档定义 XHTML1.0的文档定义 XHTML1.0标记格式 12.1 什么是XML XML中文翻译为可扩展标记语言,顾名思义,它比HT ...

  10. 使用VisualSVN Server搭建SVN服务器(转载)

    转载于http://www.cnblogs.com/greywolf/archive/2013/01/28/2879952.html 使用 VisualSVN Server来实现主要的 SVN功能则要 ...