c# 实现网页上用户自动登陆|asp.net 模拟网站登录
2010-05-06 13:58
1106 查看
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;
}
}
}
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 模拟网站登录
- c# winform实现网页上用户自动登陆,模拟网站登录
- c# winform实现网页上用户自动登陆,模拟网站登录
- 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站
- asp.net利用cookie保存用户密码实现自动登录的方法
- (微信公众号开发《一》OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆)http://blog.csdn.net/liaohaojian/article/details/70175835
- asp.net利用cookie保存用户密码实现自动登录的方法
- ASP.NET(c#)实现防止同一用户同时登陆
- C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站
- C#.NET应用程序实现网页自动登录
- 详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- c#模拟网页实现12306登陆、自动刷票、自动抢票完全篇
- 如何在 ASP.NET 应用程序中实现模拟用户身份(在ASP.NET中以管理员身份运行网站)
- ASP.NET(c#)实现防止同一用户同时登陆
- ASP.NET(c#)实现防止同一用户同时登陆
- 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站
- C#.NET实现网页自动登录的方法
- 【转】详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- 演练:ASP.NET控件实现 创建具有成员资格和用户登录功能的网站
- c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇