HttpClient 与 HttpWebRequest [英] HttpClient vs HttpWebRequest

查看:24
本文介绍了HttpClient 与 HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大文件,我必须将其发送到 Web api 客户端...数据是多部分的.问题是,如果文件是通过 http web 请求发送的,那么它会在 webapi 上快速上传.对于这个请求,文件内容直接写在请求流上.

I have a large file which I have to send to a web api client...The data is multi part. The issue is , if the file is sent over http web request, then it is uploaded quickly on the webapi. For this request, file contents are written over the request stream directly.

同一个文件是通过 Httpclient (.net 4.5) 发送的,与 http web 请求相比,上传速度很慢.我在 Httpclient post async 中使用 multipartformdatacontent.

Where as if the same file is sent over Httpclient (.net 4.5), the upload is slow when compared to http web request. I am using multipartformdatacontent in Httpclient post async.

那么,对于大文件,我们是否必须只使用 Web 请求?或者 Httpclient 上是否有任何设置可以加快上传速度?

So, for large files, do we have to use only web request? or is there any settings on Httpclient that makes the upload faster?

推荐答案

HttpClient 更像是一个无头浏览器.如果您要创建许多 http 请求,它是一个强大而理想的工具.例如,您可以设置默认标题和内容.以下是它与 这里

HttpClient is more like a head-less browser. It a powerfull and ideal tool if you are going to be creating many http request. For example you can set default headers and stuff. Here are the top 5 ways it differs from an HttpWebRequest which is taken from here

  1. HttpClient 实例是配置扩展、设置默认标头、取消未完成请求等的地方.
  2. 您可以通过单个 HttpClient 实例发出任意数量的请求.
  3. HttpClient 不绑定到特定的 HTTP 服务器或主机;您可以使用同一个 HttpClient 实例提交任何 HTTP 请求.
  4. 您可以从 HttpClient 派生为特定站点或模式创建专门的客户端
  5. HttpClient 使用新的面向任务的模式来处理异步请求,使管理和协调多个未完成的请求变得更加容易.

这篇关于HttpClient 与 HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆