简体   繁体   English

Dart中http和HttpClient的区别

[英]Difference between http and HttpClient in Dart

I'm trying to understand the difference between the http package and the HttpClient class in the dart:io library.我试图了解 dart:io 库中的http 包HttpClient 类之间的区别。 My purpose is to known when I should use which.我的目的是知道什么时候应该使用哪个。 I see both of them used to apparently do the same thing.我看到他们两个过去显然都做同样的事情。

I've seen these Q&As:我看过这些问答:

This is what I think is true so far but my understanding is fuzzy:到目前为止,这是我认为正确的,但我的理解是模糊的:

  • http is high level and HttpClient is low level ( source ) http为高级, HttpClient为低级(来源
  • http can make post requests but HttpClient can't ( source ) http可以发出 post 请求,但HttpClient不能( 来源
  • both http and HttpClient (with HttpClientRequest ) can make GET and POST requests ( source ) httpHttpClient (使用HttpClientRequest )都可以发出 GET 和 POST 请求( 来源
  • both http and HttpClient can be used on the client and the server httpHttpClient都可以在客户端和服务器端使用

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 .所以总结一下,我想说每个人都可以做其他人可以做的任何事情,但是使用http包更容易,因为这个包更高级 Is that summary correct?这个总结对吗?

The http package is an abstraction over dart:io and dart:html . http包是对dart:iodart:html的抽象。

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.所以如果你想在浏览器和其他发出 HTTP 请求的平台之间共享代码,那么最好使用http包。 Then the code will just work everywhere.然后代码将在任何地方都有效。

If you don't care about the browser use what API you like best.如果您不关心浏览器,请使用您最喜欢的 API。 In Flutter the http package just wraps dart:io 's HttpClient .在 Flutter 中, http包只是包装了dart:ioHttpClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM