WebClient vs HttpClient vs HttpWebRequest
转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/
Just when I was starting to get used to call WebServices through WSDL – like I showed here and here – I had to call a RESTful API. If you don’t know what I’m talking about you’re like me a week ago. Let’s just say that:
- a WSDL API uses SOAP to exchange XML-encoded data
- a REST API uses HTTP to exchange JSON-encoded data
That’s a whole new paradigm. Instead of GetObject()
and SetObject()
methods you have a single url api/object
that may receive either an HTTP GET
request or an HTTP POST
request.
The .NET framework offers you three different classes to consume REST APIs: HttpWebRequest
, WebClient
, HttpClient
. To worsen your analysis paralysisthe open-source community created yet another library called RestSharp
. Fear not, I’ll ease your choice.
In the beginning there was… HttpWebRequest
This is the standard class that the .NET creators originally developed to consume HTTP requests. Using HttpWebRequest
gives you control over every aspect of the request/response object, like timeouts, cookies, headers, protocols. Another great thing is that HttpWebRequest
class does not block the user interface thread. For instance, while you’re downloading a big file from a sluggish API server, your application’s UI will remain responsive.
However, with great power comes great complexity. In order to make a simple GET
you need at least five lines of code; we’ll see WebClient
does it in two.
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://example.com");
WebResponse response = http.GetResponse();
MemoryStream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();
The number of ways you can make a mistake with HttpWebRequest
is truly astounding. Only use HttpWebRequest
if you require the additional low-level control that it offers.
WebClient. Simple.
WebClient
is a higher-level abstraction built on top of HttpWebRequest
to simplify the most common tasks. Using WebClient
is potentially slower (on the order of a few milliseconds) than using HttpWebRequest
directly. But that “inefficiency” comes with huge benefits: it requires less code, is easier to use, and you’re less likely to make a mistake when using it. That same request example is now as simple as:
var client = new WebClient();
var text = client.DownloadString("http://example.com/page.html");
Note: the using statements from both examples were omitted for brevity. You should definitely dispose your web request objects properly.
Don’t worry, you can still specify timeouts, just make sure you follow this workaround.
HttpClient, the best of both worlds
HttpClient
provides powerful functionality with better syntax support for newer threading features, e.g. it supports the await
keyword. It also enables threaded downloads of files with better compiler checking and code validation. For a complete listing of the advantages and features of this class make sure you read this SO answer.
The only downfall is that it requires .NET Framework 4.5, which many older or legacy machines might not have.
WebClient vs HttpClient vs HttpWebRequest的更多相关文章
- 如何选择 WebClient,HttpClient,HttpWebRequest
当我们在用 .NET 调用 RestAPI 时通常有三种选择,分别为:WebClient, HttpWebRequest,HttpClient,这篇文章我们将会讨论如何使用这三种方式去调用 RestA ...
- HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别
HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...
- webrequest、httpwebrequest、webclient、HttpClient 四个类的区别
一.在 framework 开发环境下: webrequest.httpwebreques 都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...
- .Net5下WebRequest、WebClient、HttpClient是否还存在使用争议?
WebRequest.WebClient.HttpClient 是C#中常用的三个Http请求的类,时不时也会有人发表对这三个类使用场景的总结,本人是HttpClient 一把梭,也没太关注它们的内部 ...
- C#获取网页内容 (WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
获取网页数据有很多种方式.在这里主要讲述通过WebClient.WebBrowser和HttpWebRequest/HttpWebResponse三种方式获取网页内容. 这里获取的是包括网页的所有信息 ...
- C#中HttpWebRequest、WebClient、HttpClient的使用
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 ...
- webclient 和httpclient 应用
//webclient应用 MyImageServerEntities db = new MyImageServerEntities(); public ActionResult Index() { ...
- WebClient和HttpClient, 以及webapi上传图片
httppost请求. applicationkey/x-www-form-urlencoded请求: Email=321a&Name=kkfewwebapi里面, 如果用实体, 能接受到. ...
- C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
一.通过WebClient获取网页内容 这是一种很简单的获取方式,当然,其它的获取方法也很简单.在这里首先要说明的是,如果为了实际项目的效率考虑,需要考虑在函数中分配一个内存区域.大概写法如下 //M ...
随机推荐
- 【Python】内置函数
一.内置函数表格 详细信息 二.内置函数详情 2.1 abs(x) 返回绝对值 1 2 >>> abs(-5) 5 2.2 all(iterable) 如果这个可迭代的元素都为真,就 ...
- hihocoder 1828 Saving Tang Monk II (DP+BFS)
题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...
- day 03 字符串 for 循环
1.有变量量name = "aleX leNb" 完成如下操作: 1)移除 name 变量量对应的值两边的空格,并输出处理理结果 name = "aleX leNb&qu ...
- 洛谷 P1678 烦恼的高考志愿
题目背景 计算机竞赛小组的神牛V神终于结束了万恶的高考,然而作为班长的他还不能闲下来,班主任老t给了他一个艰巨的任务:帮同学找出最合理的大学填报方案.可是v神太忙了,身后还有一群小姑娘等着和他约会,于 ...
- MySQL主键和外键使用及说明
摘自网上一个经典的例子:大哥和小弟 一.外键约束 MySQL通过外键约束来保证表与表之间的数据的完整性和准确性. 外键的使用条件: 1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说 ...
- BZOJ2216 [Poi2011]Lightning Conductor 【决策单调性dp】
题目链接 BZOJ2216 题解 学过高中数学都应知道,我们要求\(p\)的极值,参变分离为 \[h_j + sqrt{|i - j|} - h_i \le p\] 实际上就是求\(h_j + sqr ...
- opencv透视变换GetPerspectiveTransform的总结
对于透视变换,必须为map_matrix分配一个3x3数组,除了3x3矩阵和三个控点变为四个控点外,透视变化在其他方面与仿射变换完全类似.具体可以参考:点击打开链接 主要用到两个函数WarpPersp ...
- NYOJ--69
数的长度 原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=69 分析:先看看求n!的朴素算法,用大整数乘法来实现. #include< ...
- go defer注意点,很容易出错的!!!
1:defer是在return之前执行的 函数返回的过程是这样的:先给返回值赋值,然后调用defer表达式,最后才是返回到调用函数中 返回值 = xxx 调用defer函数 空的return fun ...
- TersorflowTutorial_MNIST数据集上简单CNN实现
MNIST数据集上简单CNN实现 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow机器学习实战指南 源代码请点击下方链接欢迎加星 Tesorflow实现基于MNI ...