Dart中的HTTP和HttpClient之间的区别 [英] Difference between http and HttpClient in Dart
问题描述
我试图了解 http程序包和 HttpClient类。我的目的是知道何时应该使用它。我看到他们两个以前显然都做同样的事情。
I'm trying to understand the difference between the http package and the HttpClient class in the dart:io library. My purpose is to known when I should use which. I see both of them used to apparently do the same thing.
我已经看过这些Q& As:
I've seen these Q&As:
- 在package:html之间进行选择,dart:html,dart:io(HttpClient类)和package:http API来获取HTTP资源
- 如何在dart中使用HttpClient发出HTTPS请求?
- 在Dart控制台应用程序中,是否有不需要DOM访问的HTTP请求库?
- 如何在Dart命令行HttpClient中进行POST
- Choosing between package:html, dart:html, dart:io (class HttpClient) and package:http APIs to fetch HTTP resources
- How to make HTTPS request using HttpClient in dart?
- In a Dart console application, is there a library for an HTTP Request that doesnt require DOM access?
- How to do POST in Dart command line HttpClient
到目前为止,我认为这是正确的,但我的理解很模糊:
This is what I think is true so far but my understanding is fuzzy:
-
http
是高级别,而HttpClient
是低级别(源) -
http
可以发出发布请求,但HttpClient
不能(源) -
http
和HttpClient
(带有HttpClientRequest
)可以发出GET和POST请求(源) -
http
和HttpClient
可以在客户端和服务器上使用
http
is high level andHttpClient
is low level (source)http
can make post requests butHttpClient
can't (source)- both
http
andHttpClient
(withHttpClientRequest
) can make GET and POST requests (source) - both
http
andHttpClient
can be used on the client and the server
因此,总而言之,我想每个人都可以做其他也可以,但是使用 http
包更容易,因为这是更多高级。该摘要正确吗?
So to sum it up, I would say that each one can do anything that the other can as well, but it is easier to use the http
package since this one is more high-level. Is that summary correct?
推荐答案
http
包是对 dart:io
和 dart:html
。
因此,如果要在浏览器和其他发出HTTP请求的平台之间共享代码,则最好使用 http
包。然后,代码将随处可见。
So if you want to share code between browser and other platforms that makes HTTP requests, then it's best to use the http
package. Then the code will just work everywhere.
如果您不关心浏览器,请使用最喜欢的API。在Flutter中, http
包只包装了 dart:io
的 HttpClient
。
If you don't care about the browser use what API you like best. In Flutter the http
package just wraps dart:io
's HttpClient
.
这篇关于Dart中的HTTP和HttpClient之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!