带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 但是只支 ...
随机推荐
- Git工作常用
工作时,最好每次都创建一个本地分支,这样就会减少错误修改远程代码的机会. 基于远程dev分支创建分支并切到本地分支: git checkout -b <local_dev> -t orig ...
- cookie 和 session 机制
cookie机制 Cookie实际上是Web服务端与客户端(典型的是浏览器)交互时彼此传递的一部分内容,内容可以是任意的,但要在允许的长度范围之内.客户端会将它保存在本地机器上(如IE便会保存在本地的 ...
- css中解决img在a元素中使得a元素撑大写法
给外面a标签padding-left:; img自身float:left;margin-left:; 这种写法避免了不少因浮动产生的问题,且代码简明,推荐适当的使用此方法.
- [POJ] 3277 .City Horizon(离散+线段树)
来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...
- DPM(Deformable Parts Model)--原理(一)(转载)
DPM(Deformable Parts Model) Reference: Object detection with discriminatively trained partbased mode ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- K650D安装黑苹果
1.需要UEFI+GPT模式的win8或win10 2.关闭UEFI模式,进PE,分一个400M的分区,格式化为FAT16或EFI模式 3.制作clover模式的MAC安装U盘:链接: http:// ...
- vi基本操作
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi myfile 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command m ...
- 【转】ios的控件UIScrollView的位置定位---------逐渐积累
原文网址:http://blog.csdn.net/z343929897/article/details/8106408 UIScrollView的判断位置的属性如下: contentSize:C ...
- 使用四种框架分别实现百万websocket常连接的服务器
著名的 C10K 问题提出的时候, 正是 2001 年.这篇文章可以说是高性能服务器开发的一个标志性文档,它讨论的就是单机为1万个连接提供服务这个问题,当时因为硬件和软件的**,单机1万还是一个非常值 ...