c# 实现网页上用户自动登陆|asp.net 模拟网站登录

摘要:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Net;usingSystem.IO;namespaceCzt.Web{//////实现网站登录类///publicclassPost{//////网站Cookies///pri

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# 实现网页上用户自动登陆|asp.net 模拟网站登录》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇RabbitMQ技术详解(转)防止表单重复提交的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Mybatis

JDBCJDBC相关概念 JAVA程序都是通过JDBC连接数据库的,通过SQL对数据库编程,JDBC是由SUN公司提出的一些列规范,只定义了接口规范,具体实现由各个数据库厂商去实现,它是一种典型的桥接模式。 桥接模式是一种结构型设计模式,它的主要特点是把抽象与行为实现分离开来,分别定义接口,可以保持各部分的独立性以及应对他们的功能扩展。 JDBC规范...

java实现文件压缩与解压

用java实现文件的压缩与解压是很常见的功能。 我最爱上代码:    1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import net.lingala.zip4j.core.ZipFile; 6 import ne...

springboot后端实现条件查询,要配合使用mybatis

packagecn.com.dyg.work.sqlgen; importcn.com.dyg.work.common.exception.DefException; importcn.com.dyg.work.common.utils.CamelAndUnderLineConverter; importcom.alibaba.fastjson.JSON...

Scala入门系列(十一):模式匹配

引言 模式匹配是Scala中非常有特色,非常强大的一种功能。 类似于Java中的switch case语法,但是模式匹配的功能要比它强大得多,switch只能对值进行匹配,但是Scala的模式匹配除了可以对值进行匹配之外,还可以对类型进行匹配、对Array和List的元素情况进行匹配、对case class进行匹配甚至对有值或没值(Option)进行匹配...

mybatisplus批量插入编辑及sql打印

一、自定义数据方法注入 EasySqlInjector  import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.my...

.Net使用Redis详解之ServiceStack.Redis(七)

序言本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习。 Redis官方推荐的.NET驱动类库为Service.Stack.Redis。然而网上对这个类库的中文文档不是很全面与合理,这篇文章主要就对这个类库做注释展现。不足遗漏...