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. GIS数据格式:Geodatabase

    转自:http://www.cnblogs.com/quansixiang/archive/2010/09/17/1829286.html 1  Geodatabase概念 Geodatabase是A ...

  2. 关于offset()的理解

    假如要取得x线的offset().top,在页面上的是x线到页面顶端的距离s1,如何取得x线在窗口上的top呢,这就需要取得窗口顶端到页面顶端的距离s2,由于s1和s2都是有方向的,所以,s1-s2就 ...

  3. c# PrintDocument 设置自定义纸张大小的示例

    .Net 提供的打印类PrintDocument 非常简洁易用,不过在实际应用开发中往往需要对纸张进行自定义,尤其是需要进行票据打印时.这个问题也困扰了我许久,经过查阅相关的资料和多次尝试,发现 其实 ...

  4. 查询显示MSSQL表结构 [转]

    SELECT 表名 = Case When A.colorder= Then D.name Else '' End, 表说明 = Case When A.colorder= Then isnull(F ...

  5. Asp.Net MVC4新特性指南(2):新特性介绍

       上一章讲解了最基本的MVC4说明.今天就介绍下几种新特性的使用例子:   就当大家有MVC3的基础了.在这个基础上在看下面的介绍就容易多了.1.Web API MVC4包括一个更好的解决方案:A ...

  6. 不同的jar里边相同的包名类名怎么区别导入

    今天在做项目的时候遇到了一个很有意思的问题,折磨了我很长时间,不过最终还是解决了,特留此文纪念一下. 遇到的问题: 同样一段代码,在同事那就好使,在我这就找不到一个方法.引用的包也都是相同的,这种问题 ...

  7. Esper系列(一)初探

    Esper介绍 Esper是一个Java开发并且开源的轻量级和可扩展的事件流处理和复合事件处理引擎,并提供了定制的事件处理语言(EPL). 应用场景 某个用户在请求登录服务时,n秒内连续m次未登录成功 ...

  8. PAT 1038 体验Python之美

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  9. 表达式计算器(LL1文法)

    LL(1)文法求算数表达式的值递归子程序法 分析过程: 表达式文法G[E]:E->E+T|E-T|TT->T*F|T/F|T%F|FF->N^F|NN->(E)|NUM|+NU ...

  10. ListBox获取行字符串

    ListBox获取行字符串 关键点 获取ListBox第1行的字符串 获取ListBox第2行的字符串 获取ListBox第n行的字符串 CListBox::GetText int GetText( ...