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, , 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();
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();
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, , 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();
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 > )
{
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, , 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();
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();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
co = new CookieContainer();
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
if (cookieContainer.Count > )
{
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#模拟登录的htmlHelper类的更多相关文章

  1. Python爬虫实战五之模拟登录淘宝并获取所有订单

    经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...

  2. Python 爬虫实战5 模拟登录淘宝并获取所有订单

    经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 本篇内容 python模拟登录淘宝网页 获取登录用户的所有订单详情 ...

  3. HttpWebRequest 模拟登录响应点击事件(分享自己用的HttpHelper类)

    平时也经常采集网站数据,也做模拟登录,但一般都是html控件POST到页面登录:还没有遇到用户服务器控件button按钮点击事件登录的,今天像往常一样POST传递参数,但怎么都能登录不了:最后发现还有 ...

  4. C# 实现模拟登录功能,实现公共类分享。

    前言 最近在研究模拟登录的各种方法, 主要想要实现的两个功能是: 1.点击按钮可以直接跳转并登录到某一个系统中. 2.抓取某一个系统中某一个页面中的特定数据. 为此在网上查了许多的资料,首先了解到自身 ...

  5. 模拟登录神器之PHP基于cURL实现自动模拟登录类

    一.构思 从Firefox浏览器拷贝cURL命令(初始页.提交.提交后) 自动分析curl形成模拟登录代码 默认参数:ssl/302/gzip 二.实现 接口 (一)根据curl信息执行并解析结果 p ...

  6. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  7. .NET微信模拟登录及{base_resp:{ret:-4,err_msg:nvalid referrer}}的解决办法

    12年的时候写了些关于微信开发的内容,当时看好这个东西,可惜当时腾讯开放的权限太少,之后的一年多时间没有太关注了. 现在又要重新开始微信开发的阵容了,微信只是个入口,微网站才是趋势. 我是个水货,所以 ...

  8. .net 模拟登录Post提交

    最近在做一个项目,要求集成到第三方系统中,由于先前没有做过类似的活,所以折腾了几天,蹭着有闲情的时候记录一下. 以下实例,都是我用Asp.net语言进行开发的,关于HTML元素的获取,使用的是Goog ...

  9. 使用ImitateLogin模拟登录百度

    在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...

随机推荐

  1. 【学习笔记】【oc】类和对象及类的三大基本特征

    1.类和对象 类是抽象化,对象是具体化. (1)定义类: 分为两个步骤,类的声明:定义类的成员变量和方法:@interface 用于声明定义类的接口部分,@end表面定义结束:. 成员变量的定义:{} ...

  2. 一条sql,有分页,表合并查询,多表连接,用于oracle数据库

    SELECT * FROM ( SELECT TT.*,ROWNUM RN FROM ( SELECT A.CASE_ID AS TREATID, A.TYPE AS TYPE, B.CONTENT ...

  3. Windows下python安装matplotlib

    此文为转载,原文地址为:http://blog.csdn.net/u010585135/article/details/42127273 一.下载matplotlib安装包:网址http://matp ...

  4. android更新SDK时候丢失annotations.jar 导致支持库报错

    I am trying to update my Android SDK Tools to 17 rev. and I updated usign SDK Tools but in Propertie ...

  5. LED限流电阻的大小计算

    很多时候电路中都用LED做指示,这就涉及到限流电阻,这个怎么选取呢? 可以举个例子,贴片蓝色LED datasheet上参数如下 要注意理解LED Datasheet上的参数.最重要的三个参数如下: ...

  6. mysql System Tablespace

    System Tablespace 数据文件配置: mysql> show variables like '%innodb_data_file_path%'; +---------------- ...

  7. JAVA并发2

    Java 5中引入了新的锁机制--java.util.concurrent.locks中的显式的互斥锁:Lock接口,它提供了比synchronized更加广泛的锁定操作.Lock接口有3个实现它的类 ...

  8. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  9. 深入理解linux网络技术内幕读书笔记(九)--中断与网络驱动程序

    Table of Contents 1 接收到帧时通知驱动程序 1.1 轮询 1.2 中断 2 中断处理程序 3 抢占功能 4 下半部函数 4.1 内核2.4版本以后的下半部函数: 引入软IRQ 5 ...

  10. Xshell远程连接Ubuntu

    在Ubuntu系统下执行命令: sudo apt-get install openssh-server 然后,在Xshell中新建会话,输入Ubuntu系统的IP,以及用户名.密码. 但是经常会出现连 ...