怎么利用C#中的 webclient 创建cookie
2023-09-11 14:21:33 时间
Cookies are not limited only to web browsers. any http-aware client that supports cookies can deal with a cookie sending aSp .net Web api. the following code example shows a class extended from WebClient. it overrides the virtual method GetWebRequest to attach an instance of CookieContainer to the request. the CookieContainer object instance has to be reused across the requests to let it push cookies in the subsequent requests. For this reason, it is a class-level field and the same instance of the web client is used to send multiple requests. here we use a proxy of address localhost and port 8888, that of Fiddler, to inspect requests and responses.
public class CookieWebClient : WebClient { private CookieContainer jar = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); HttpWebRequest webRequest = request as HttpWebRequest; if (webRequest != null) webRequest.CookieContainer = jar; return request; } } public class TestClass { public void MyTestMethod() { string url = "http://localhost:7077/api/employees/12345"; CookieWebClient client = new CookieWebClient() { Proxy = new WebProxy("localhost", 8888) // Fiddler }; Console.WriteLine(client.DownloadString(url)); // In this request, the cookie gets sent back to the web API } }
相关文章
- C#-Cookie
- Protected vs protected internal (Again) in c#
- [译]聊聊C#中的泛型的使用(新手勿入) Seaching TreeVIew WPF 可编辑树Ztree的使用(包括对后台数据库的增删改查) 字段和属性的区别 C# 遍历Dictionary并修改其中的Value 学习笔记——异步 程序员常说的「哈希表」是个什么鬼?
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
- 请求大神,C#如何截取字符串中指定字符之间的部分 按指定字符串分割 一分为二 c# 去除字符串中的某个已知字符
- 利用反射快速给Model实体赋值 使用 Task 简化异步编程 Guid ToString 格式知多少?(GUID 格式) Parallel Programming-实现并行操作的流水线(生产者、消费者) c# 无损高质量压缩图片代码 8种主要排序算法的C#实现 (一) 8种主要排序算法的C#实现 (二)
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- 【卷土重来之C#学习笔记】(二)c#编程概述
- Cookie-Parser是怎样解析签名后的cookie的(同一时候对cookie和cookie-signature进行说明)
- Word处理控件Aspose.Words功能演示:使用 C# 拆分 MS Word 文档
- 解决Cookie在其他页面获取不到,在一个页面设置cookie另一个页面获取不到
- jsoup header携带Cookie访问需要cookie才能加载的网页
- C#,图像的平均哈希(Average hash),感知哈希(Perceptual hash)与差异哈希(Difference hash)算法与源代码
- C#,布尔表达式括号问题(Boolean Parenthesization Problem)的求解算法与源代码
- C#中Tuple的使用
- Cookie的使用(js-cookie插件)
- 一个完整的类用来读取OpenSSL生成的pem格式的x509证书(C#)
- 《C#零基础入门之百识百例》(十六)循环结构do-while语句 -- 这道题会做了吗?
- C# 委托和事件简介
- C#属性的定义
- Cookie的使用(js-cookie插件)
- C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
- C#多线程的实现
- C# 计算接口调用时长
- c#代码规则,C#程序中元素的命名规范