簡體   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