C#模拟登录的htmlHelper类
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类的更多相关文章
- Python爬虫实战五之模拟登录淘宝并获取所有订单
经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 温馨提示 更新时间,2016-02-01,现在淘宝换成了滑块验证了 ...
- Python 爬虫实战5 模拟登录淘宝并获取所有订单
经过多次尝试,模拟登录淘宝终于成功了,实在是不容易,淘宝的登录加密和验证太复杂了,煞费苦心,在此写出来和大家一起分享,希望大家支持. 本篇内容 python模拟登录淘宝网页 获取登录用户的所有订单详情 ...
- HttpWebRequest 模拟登录响应点击事件(分享自己用的HttpHelper类)
平时也经常采集网站数据,也做模拟登录,但一般都是html控件POST到页面登录:还没有遇到用户服务器控件button按钮点击事件登录的,今天像往常一样POST传递参数,但怎么都能登录不了:最后发现还有 ...
- C# 实现模拟登录功能,实现公共类分享。
前言 最近在研究模拟登录的各种方法, 主要想要实现的两个功能是: 1.点击按钮可以直接跳转并登录到某一个系统中. 2.抓取某一个系统中某一个页面中的特定数据. 为此在网上查了许多的资料,首先了解到自身 ...
- 模拟登录神器之PHP基于cURL实现自动模拟登录类
一.构思 从Firefox浏览器拷贝cURL命令(初始页.提交.提交后) 自动分析curl形成模拟登录代码 默认参数:ssl/302/gzip 二.实现 接口 (一)根据curl信息执行并解析结果 p ...
- Python requests模拟登录
Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...
- .NET微信模拟登录及{base_resp:{ret:-4,err_msg:nvalid referrer}}的解决办法
12年的时候写了些关于微信开发的内容,当时看好这个东西,可惜当时腾讯开放的权限太少,之后的一年多时间没有太关注了. 现在又要重新开始微信开发的阵容了,微信只是个入口,微网站才是趋势. 我是个水货,所以 ...
- .net 模拟登录Post提交
最近在做一个项目,要求集成到第三方系统中,由于先前没有做过类似的活,所以折腾了几天,蹭着有闲情的时候记录一下. 以下实例,都是我用Asp.net语言进行开发的,关于HTML元素的获取,使用的是Goog ...
- 使用ImitateLogin模拟登录百度
在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...
随机推荐
- 下拉列表框Combo Box
Combo Box/Combo Box Ex 组合窗口是由一个输入框和一个列表框组成.创建一个组合窗口可以使用成员函数: BOOL CListBox::Create( LPCTSTR lpszText ...
- express4.x中的链式路由句柄
var express = require("express"); var router = express(); router.get('/', function (req, r ...
- 个人vim配置(.vimrc文件分享)
syntax enable syntax on colorscheme desert set nu! set nowrap set nobackup set backspace= set tabsto ...
- (摘)Chart属性设置
Chart ChartBorder 图表区域的边框设置 ChartFill 图表区域的背景填充 Legend 图表的注释标签显示设置项目,一组数据对应一种颜色的注释 IsHStack 当有多个显示项的 ...
- microsoft
http://blog.csdn.net/morewindows/article/details/8684061 http://blog.csdn.net/watkinsong/article/det ...
- Web2py也有意思的
多学学,以后可以方便的自己写代码了. 对于各种WEB框架,这也是打一个基础的时候. 相信学入门了,对PHP的,JAVA的WEB框架,都是能理解更深入的. def index(): "&quo ...
- Effective C++笔记(一)——条款26-29
条款26:尽可能延后变量定义式的出现时间 为何要尽量延后? 当程序中途跳出而导致变量未被使用,但是必须进行构造和析构. 最佳初始化变量 直接在构造时指定初值比构造之后再赋值效率高(条款4) ... s ...
- class$1,class$2,class$innerclass中的$的含义
class文件名中的$的含义如下: $后面的类是$前面的类的内部类 内部类有以下两种情况: 1.普通的组合类形式,即在一个类内部定义一个普通的类 public class Outer { cla ...
- ZOJ2112--Dynamic Rankings (动态区间第k大)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 使用MapReduce将HDFS数据导入到HBase(一)
package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...