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. git add 之后因为没提交正确文件需要撤销

    之后因为没提交正确文件需要撤销时,采用如下方法:git log //查看提交记录MT6592_L_2SIM_GIT$ git reset --hard 717af7c9664be098939cb266 ...

  2. 自定义View(9)关于onLayout

    1,何时被调用 当外层容器组件调用其内部组件的layout(l,r,t,b)时,内部组件的onLayout就被调用.与onMeasure类似. 2,注意 onLayout对ViewGroup及子类才有 ...

  3. [CCF2015.12]题解

    201512-1 数位之和 水题一个,取模除以10胡搞即可(不知道字符串为什么不行 #include <algorithm> #include <iostream> #incl ...

  4. leetcode:Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. This project needs to migrate WTP metadata

    in command-line: path> mvn eclipse:clean path> mvn -Dwtpversion=1.5 eclipse:eclipse path> m ...

  6. ACM - ICPC World Finals 2013 H Матрёшка

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...

  7. [ionic开源项目教程] - 第1讲 前言,技术储备,环境搭建,常用命令

    前言 这是一个系列文章,将持续更新到项目完结,从环境搭建开始讲解,包括实战开发中遇到的各种问题的解决方案,都将毫无保留的分享给大家. 技术储备 开始本项目之前,请确保自己对以下技术点都有所了解. ht ...

  8. JAVA字符串格式化-String.format()的使用 (转载)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  9. Hack 【二分答案】

    题意:给出n门课程,每一门课程考的分数,每一门课程的学分,求最多删去k组数据之后能够得到的最大加权平均数 先开一个数组x[],其中x[i]=1代表没有删除这门课程,x[i]=0表示删除了这门课程 然后 ...

  10. 【网络】js调试F12控制台学习

    http://www.cnblogs.com/52cik/p/js-console-acquaintance.html console.log 输出信息 http://javascript.ruany ...