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", ) // Fiddler
};
Console.WriteLine(client.DownloadString(url)); // In this request, the cookie gets sent back to the web API
}
}

怎么利用C#中的 webclient 创建cookie的更多相关文章

  1. 如何合理利用iMindMap中的模板创建思维导图

    思维导图的制作并不是一项简单的工作,尤其是对许多工作或学习有特殊要求的朋友而言,当我们需要应对不同场景制作不同的思维导图时,总不能都靠自己从头制作,这样难度比较大也比较耗时.而iMindMap(win ...

  2. 如何利用ZBrush中的DynaMesh创建身体(二)

    之前的ZBrush教程我们在了解了人体比例和结构的前提下,使用ZBrush®软件中的Append功能和InsertSphere笔刷添加躯干.本讲将参照图片继续对“亡灵僵尸”的形体结构进行细致刻画和使用 ...

  3. 如何利用ZBrush中的DynaMesh创建身体(一)

    之前的ZBrush教程中我们用Extract抽出功能演示了头发的立体雕刻方法,本讲将对已完成的头部模型添加躯干,使用DynaMesh创建身体的方法,以及人体比例和结构的介绍. 查看详细的视频教程可直接 ...

  4. 利用iOS中Safari浏览器创建伪Web App

    在safari浏览器里有一个“添加到主屏幕”选项,我们可以用来创建伪Web App,下面来了解一下iOS中Safari的私有属性 第一步设置Web App的主屏幕图标: 有两种属性值apple-tou ...

  5. zk框架中利用map类型传值来创建window,并且传值

    @Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...

  6. jsp利用cookie记住用户名,下次登录时显示在文本框中(仅仅一个Cookie就整了将近三个小时,⊙﹏⊙b汗)

    <%@page import="java.net.URLDecoder"%> <%@page import="sun.security.util.Len ...

  7. 从WebBrowser中取得Cookie 和 WebClient设置cookie!

    原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...

  8. WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务

    原文:WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务 在<基于IIS的WCF服务寄宿(Hosting)实现揭秘>中,我们谈到在采用基于IIS(或者 ...

  9. JAVA中的Session和Cookie【转】

    一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于才服务器端保持状态的 ...

随机推荐

  1. leetcode:Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 【笨嘴拙舌WINDOWS】Dj,oh!nonono,It is about DC

    “DC: Device content 设备描述表.通常指显示器,或者打印机设备的描述” 如果你不是从事打印机方面的编程,那么就可以将DC简单的理解为显示器的属性表.WINDOWS将内存里面的东西通过 ...

  3. LA 3644 X-Plosives

    最简单的并查集 多做做水题,加深一下理解 //#define LOCAL #include <cstdio> + ; int parent[maxn]; int GetParent(int ...

  4. 漫游Kafka设计篇之性能优化

    Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...

  5. LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

    题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...

  6. Mac 上安装MySQL

    http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/ http://www.wkii.org/mac-os-x-ins ...

  7. Linux apt-get

    apt-get apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. apt-get命令一般需要root权限执行, ...

  8. HDU 4766 Network

    题意   就是找到一个 位置 使得路由器可以覆盖所有英雄    可以不覆盖主人自己, 找到距离 主人房子最近的位置,距离为多少 没想到  其实是道水题啊!!  分三个情况讨论 第一个情况    如果 ...

  9. 使用 Linux 终端 SSH 登录 VPS

    Windows 中远程 SSH 登录 VPS 进行管理的利器是 PuTTY,但是 Linux 中就没必要用它了.Linux.Unix(包括 Mac iOS)都必然有内置的命令行终端,内建了 OpenS ...

  10. 内存泄露(OOM)现象及举例

    一.HeapSize OOM(堆空间内存溢出) A.eg:List.add(" ")在一个死循环中不断的调用add却没有remove. B.并发导致. 解决方法有:1.代码提速.这 ...