繁体   English   中英

什么会影响使用 C# 中的 REST API 的 HTTP POST 的响应时间

[英]What can affect the response time for an HTTP POST using a REST API in C#

我正在尝试使用 C# 后端制作一个简单的 REST Web API。

我的主要问题是:如果我使用一个非常大的文件,为什么我的 Web 客户端需要很长时间才能显示收到响应? 无论文件大小如何,我的返回响应的方法似乎都很快完成。

下面是我的代码以及输出和两个不同的 HTTP POST 语句及其结果。 在我的第一个示例中,我使用了一个 246 KB 的文件,并在 20 毫秒内在客户端获得响应。 第二个示例使用一个 2089344 KB 的文件,并在大约 45 秒后在客户端获得响应。 两个日志都显示从方法开始到创建结果之间只有几毫秒。

任何帮助表示赞赏! 谢谢。

代码:

[HttpPost, Route("test/upload")]
    public HttpResponseMessage Post([FromUri]string filename)
    {


        var task = Request.Content.ReadAsStreamAsync();
        System.Diagnostics.Debug.WriteLine("1: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        task.Wait();
        System.Diagnostics.Debug.WriteLine("2: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        Stream requestStream = task.Result;


        try
        {
            System.Diagnostics.Debug.WriteLine(filename);
            System.Diagnostics.Debug.WriteLine("3: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
            requestStream.Close();

        }
        catch (IOException)
        {
            requestStream.Close();
            throw new HttpResponseException(HttpStatusCode.InternalServerError);
        }
        System.Diagnostics.Debug.WriteLine("4: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.Created);
        System.Diagnostics.Debug.WriteLine("5: " + (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
        return result;
    }

第一个 HTTP POST:

POST /test/upload?filename=test HTTP/1.1
Content-Length: 252335
Host: localhost:64105
Content-Type: application/octet-stream

[message-body; type: application/octet-stream, size: 252335 bytes]

第一反应:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?BQzpcVXNlcnNcSWFuXERvY3VtZW50c1xXb3JraW5nRm9sZGVyXFNpbXBsZVJlc3RcU2ltcGxlUmVzdFx0ZXN0XHVwbG9hZA==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Oct 2017 17:50:07 GMT
Content-Length: 0

第一个日志:

1: 63642721970786
2: 63642721970788
test
3: 63642721970791
4: 63642721970792
5: 63642721970793

第二个 HTTP POST:

POST /test/upload?filename=test HTTP/1.1
Content-Length: 2139488256
Host: localhost:64105
Content-Type: application/octet-stream

[message-body; type: application/octet-stream, size: 2139488256 bytes]

第二个回应:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcSWFuXERvY3VtZW50c1xXb3JraW5nRm9sZGVyXFNpbXBsZVJlc3RcU2ltcGxlUmVzdFx0ZXN0XHVwbG9hZA==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Oct 2017 17:56:07 GMT
Content-Length: 0

第二个日志:

1: 63642722167480
2: 63642722167481
test
3: 63642722167484
4: 63642722167485
5: 63642722167487

看起来您没有对requestStream做任何工作。 在打印文件名之前添加此内容,您应该会看到时差。

new StreamReader(requestStream).ReadToEndAsync().Wait();

至于为什么你现在没有看到时差,在调用控制器之前将整个请求发送到服务器。 请参阅此问题以获取流式传输请求而不是等待整个主体的解决方案。

ASP.Net Web API:在读取/上传请求正文之前发送响应

暂无
暂无

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

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