简体   繁体   中英

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

I am trying to make a simple REST Web API with a C# back-end.

My main question is: Why does it take so long for my web client to show a response was received if I use a very large file? My method returning the response seems to complete very quickly regardless of file size.

My code is below as well as the output and the two different HTTP POST statements and their results. In my first example I use a file that is 246 KB and get the response on the client side within 20ms. The second example uses a file that is 2089344 KB and gets a response on the client side about 45 seconds later. Both logs show that there are only a few milliseconds between the beginning of the method and the result being created.

Any help is appreciated! Thanks.

CODE:

[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;
    }

FIRST 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]

FIRST RESPONSE:

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

FIRST LOG:

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

SECOND 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]

SECOND RESPONSE:

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

SECOND LOG:

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

It looks like you're not doing any work with requestStream . Add this before your print out the filename and you should see a time difference.

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

As for why you're not seeing a time difference now, the entire request is sent to the server before the controller is called. See this question for a solution to streaming the request instead waiting for the whole body.

ASP.Net Web API: Send response before request body is read/uploaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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