带Cookie的 WebClient
/// <summary>
/// WebClient的扩展
/// </summary>
public class webClient : WebClient
{
/// <summary>是否重定向 默认为true</summary>
public bool AllowAutoRedirect { get; set; }
/// <summary>
///获取或设置 Referer HTTP 标头的值。默认值为 null。
/// </summary>
public string Referer { get; set; }
/// <summary>
/// 获取或设置 Content-type HTTP 标头的值。 默认: application/x-www-form-urlencoded
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// 获取或设置 User-agent HTTP 标头的值。默认:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
/// </summary>
public string UserAgent { get; set; } /// <summary>
/// 获取或设置 GetResponse 和 GetRequestStream 方法的超时值(以毫秒为单位)。 默认值是 30,000 毫秒(30 秒)。
/// </summary>
public int Timeout { get; set; }
/// <summary>
/// 获取或设置写入或读取流时的超时(以毫秒为单位)。默认值为 300,000 毫秒(5 分钟)
/// </summary>
public int ReadWriteTimeout { get; set; } /// <summary>获取或设置请求相关联的Cookie</summary>
public CookieContainer CookieContainer { get; set; }
/// <summary>创建一个新的 WebClient 实例。</summary>
public webClient()
{
this.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)";
this.ContentType = "application/x-www-form-urlencoded";
this.CookieContainer = new CookieContainer();
this.AllowAutoRedirect = true;
this.Timeout = * ;
this.ReadWriteTimeout = * ;
}
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
if (this.Referer != null) request.Referer = this.Referer;
if (this.ContentType != null) request.ContentType = this.ContentType;
if (this.UserAgent != null) request.UserAgent = this.UserAgent;
request.AllowAutoRedirect = this.AllowAutoRedirect;
request.CookieContainer = this.CookieContainer; //这句很关键,有了他可以保存返回的Cookie
request.Timeout = this.Timeout;
request.ReadWriteTimeout = this.ReadWriteTimeout;
return request;
}
}
带Cookie的 WebClient的更多相关文章
- postman发送带cookie的http请求
1:需求:测试接口的访问权限,对于某些接口A可以访问,B不能访问. 2:问题:对于get请求很简单,登录之后,直接使用浏览器访问就可以: 对于post请求的怎么测试呢?前提是需要登录态,才能访问接口. ...
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理
原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...
- 从WebBrowser中取得Cookie 和 WebClient设置cookie!
原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...
- HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式
HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式 参考路径:https://www.cnblogs.com/splendidme/archive/2011/09/14/ ...
- postman 请求带cookie
以亚马逊为例,我抓包随便看一个返回是json数据格式的一个接口,比如随便点一个,我的订单 随便找一条,然后复制url过滤 右键,copy下url 将url放入filter过滤: 在postman里面, ...
- QtQuick大坑笔记之Http的Get与Post操作(带cookie)
前言 最近在为单位做一个简单的手机App,基于Qt技术栈的选择了QtQuick来开发.不得不说QtQucik开发的确舒服,很多东西都不用写就可以只用,UI定义起来也比较自由.但是本人想通过cookie ...
- urllib2 post请求方式,带cookie,添加请求头
#encoding = utf-8 import urllib2import urllib url = 'http://httpbin.org/post'data={"name": ...
- PostMan 使用Interceptor 发送带cookie的请求一直loading
问题 最近要写一个爬虫(虽然是第一次写),于是就用了Chrome上非常方便一个插件,PostMan,但是由于chrome安全的限制,发不出带cookie和带有自定义头部标签的请求. 百度一番后得如果想 ...
- asp.net web api 跨域,带cookie
官网上有一个介绍 http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 但是只支 ...
随机推荐
- stdobj to array php
The lazy one-liner method You can do this in a one liner using the JSON methods if you're willing to ...
- jquery ui tab标签
<!DOCTYPE html> <html> <head> <title>tab</title> <style type=" ...
- (实用篇)PHP JSON数组与对象的理解
在PHP后端和客户端数据交互的过程中,JSON数据中有时格式不定,一会儿是数组,一会儿是对象,弄得客户端开发人员要崩溃的感觉. 因此,前后端相关人员先对PHP的json_encode函数原理有必要的了 ...
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing
来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The ...
- smartmontools的安装使用和实现对磁盘的Nagios监控
安装 首先从sourceforge下载最新的安装版本. 解压编译 $ tar -zxvf smartmontools-6.4.tar.gz $ cd smartmontools-6.4 $ ./con ...
- Asp.net 执行回调操作后 无法更新ViewState的问题
源码 待续...
- Mysql数据库搭建-Windows
首先先下载一下文件包. 下面这个文件包里面包含了已配置的MySQL5.5和连接器. https://www.dropbox.com/s/iu4ifv0m2232ktn/Win_Mysql5_ODBC. ...
- mysql优化参数thread_cache_size
thread_cache_size功能在mysql数据库配置文件中是非常重要的一项功能了,如果对thread_cache_size优化做得好我们可以让服务器跑得非常快,设置不好就会发现很小访问量就非常 ...
- hdu1285 拓扑序
题意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...