C#实现网站登录
public class HTMLHelper
{
/// <summary>
/// 获取CooKie
/// /// </summary>
/// /// <param name="loginUrl"></param>
/// /// <param name="postdata"></param>
/// /// <param name="header"></param>
/// /// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = "GET";
request.ContentType = header.contentType;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies; //Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取CooKie
/// /// </summary>
/// /// <param name="loginUrl"></param>
/// /// <param name="postdata"></param>
/// /// <param name="header"></param>
/// /// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies; //Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl,string post, CookieContainer cookieContainer, HttpHeader header,Encoding en)
{
Thread.Sleep(1000);
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
byte[] oneData = Encoding.Default.GetBytes(post);
Uri uri = new Uri(getUrl);
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL
myHttpWebRequest.CookieContainer = cookieContainer;//*发送COOKIE
myHttpWebRequest.Method = header.method;
myHttpWebRequest.ContentType = header.contentType;
myHttpWebRequest.ContentLength = oneData.Length;
Stream newMyStream = myHttpWebRequest.GetRequestStream();
newMyStream.Write(oneData, 0, oneData.Length);
try
{
httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en);
string str = sr.ReadToEnd();
string msg = string.Empty;
return str;
}
catch (Exception ex)
{
return string.Empty;
}
}
catch (Exception e)
{
if (myHttpWebRequest != null) myHttpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
return string.Empty;
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, string post, CookieContainer cookieContainer,out CookieContainer co, HttpHeader header, Encoding en)
{
Thread.Sleep(1000);
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse httpWebResponse = null;
co = new CookieContainer();
try
{
byte[] oneData = Encoding.Default.GetBytes(post);
Uri uri = new Uri(getUrl);
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL
if (cookieContainer.Count > 0)
{
myHttpWebRequest.CookieContainer = cookieContainer;
}
else
{
myHttpWebRequest.CookieContainer = co;
}
myHttpWebRequest.Method = header.method;
myHttpWebRequest.ContentType = header.contentType;
myHttpWebRequest.ContentLength = oneData.Length;
Stream newMyStream = myHttpWebRequest.GetRequestStream();
newMyStream.Write(oneData, 0, oneData.Length);
try
{
httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en);
httpWebResponse.Cookies = myHttpWebRequest.CookieContainer.GetCookies(myHttpWebRequest.RequestUri);
CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式
string strcrook = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri);
string str = sr.ReadToEnd();
string msg = string.Empty;
return str;
}
catch (Exception ex)
{
return string.Empty;
}
}
catch (Exception e)
{
if (myHttpWebRequest != null) myHttpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
return string.Empty;
}
/// <summary>
/// 获取Stream
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static Stream GetStream(string getUrl, CookieContainer cookieContainer, HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = header.referer;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
return responseStream;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return null;
}
}
public static Stream GetStream(string getUrl, CookieContainer cookieContainer, out CookieContainer co, HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
co = new CookieContainer();
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
if (cookieContainer.Count > 0)
{
httpWebRequest.CookieContainer = cookieContainer;
}
else
{
httpWebRequest.CookieContainer = co;
}
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
httpWebResponse.Cookies = httpWebRequest.CookieContainer.GetCookies(httpWebRequest.RequestUri);
CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式
string strcrook = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.RequestUri);
return responseStream;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return null;
}
}
}
public class HttpHeader
{
public string contentType { get; set; }
public string accept { get; set; }
public string userAgent { get; set; }
public string method { get; set; }
public int maxTry { get; set; }
public string referer { get; set; }
}
C#实现网站登录的更多相关文章
- cookielib和urllib2模块相结合模拟网站登录
1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...
- discuz论坛与其它网站登录注册整合
discuz论坛与其它网站登录注册整合 本文以discuz 7.0.0 php版本的论坛与 .net 2.0的网站注册登录整合为类.没有采用uc_center或第三方插件.以另类的方式实现.此方法实现 ...
- Java模拟网站登录02【转载】
如何用Java代码模拟一些如百度.QQ之类的网站登录?有两个方式,一是发送模拟请求,二是模拟浏览器操作,而这两种方式恰好在Java有开源实现,在这里介绍一个工具包,它是家喻户晓的HttpClient. ...
- c# winform实现网页上用户自动登陆,模拟网站登录
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- 搭建开发框架Express,实现Web网站登录验证
NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证 JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需 ...
- Javascript技巧实例精选(4)—FTP网站登录的Javascript实现
FTP网站登录的Javascript实现 >>点击这里下载完整html源码<< 这是最后的截图 这是核心Javascript源码 function goFtpSite() { ...
- 关于网站登录后的页面操作所携带的不同cookie值
对于课堂派网站,登录后的页面操作只需要携带PHPSESSID或者cookie中间那部分即可,两个都带也可,SERVERID不知道是干啥的,每次响应的都会变. 代码实现: cookie = None c ...
- windowsXP用户被禁用导致不能网站登录
1.查看系统事件,发现弹出如下的错误 2.根据上面的错误,我们很容易就可以判断是禁用了账户引起的 2.1后面进入计算机管理,再进入用户管理 2.2双击点开Internet来宾用于,发现此用户已经停用了 ...
- SharePoint 网站登录不上,3次输入用户名/密码白页
新搭建的SharePoint 2013环境,第一次干的这么憋屈的慌,先是接了一个Ghost的服务器,装好的服务器.Sql.SharePoint.VS等一系列,却发现怎么都登陆不上去,输入账号3次以后白 ...
- HttpClient 4.x 执行网站登录并抓取网页的代码
HttpClient 4.x 的 API 变化还是很大,这段代码可用来执行登录过程,并抓取网页. HttpClient API 文档(4.0.x), HttpCore API 文档(4.1) pack ...
随机推荐
- PyCharm搭建Spark开发环境 + 第一个pyspark程序
一, PyCharm搭建Spark开发环境 Windows7, Java 1.8.0_74, Scala 2.12.6, Spark 2.2.1, Hadoop 2.7.6 通常情况下,Spark开发 ...
- linux模块导出符号 EXPORT_SYMBOL_GPL&EXPORT_SYMBOL(转)
转自:http://blog.csdn.net/angle_birds/article/details/7396748 一个模块mod1中定义一个函数func1:在另外一个模块mod2中定义一个函数f ...
- cocos2dx游戏 地图
#include "HelloWorld.h" USING_NS_CC; CCScene* MyHelloWorld::scene() { // 'scene' is an aut ...
- 【ubantu】Ubuntu的一些常用快捷键
Ubuntu操作基本快捷键* 打开主菜单 = Alt + F1* 运行 = Alt + F2* 显示桌面 = Ctrl + Alt + d* 最小化当前窗口 = Alt + F9* 最大化当前窗口 = ...
- 在Ubuntu下利用Eclipse开发FFmpeg配置小结
首先需要编译FFmpeg得到头文件和lib文件,参见:在Ubuntu下编译FFmpeg 选择File-New-C Project 选择Executable下的Empty Project,右侧选择Lin ...
- Esper 20章 优化
20 优化esper为了处理高速的生成力已经高度优化,并且接收事件和输出结果低延迟.esper还可以进一步最大化可测使用在 软实时和硬实时JVM 上. 本章描述了最好的优化练习,而且解释了怎么去评价e ...
- vue2 本地安装
- Laravel开发:Laravel核心——Ioc服务容器
服务容器 在说 Ioc 容器之前,我们需要了解什么是 Ioc 容器. Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具. 在理解这句话之前,我们需要先了解一下服务容器的来龙去脉: ...
- python 正則表達式推断邮箱格式是否正确
import re def validateEmail(email): if len(email) > 7: if re.match("^.+\\@(\\[?) ...
- 1.Python学习---helloworld
1.首先访问http://www.python.org/download/去下载最新的python版本. 2.安装下载包,一路next. 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...