C# 在C中使用RESTAPI时,什么会影响HTTP POST的响应时间#
我正在尝试用C#后端制作一个简单的RESTWebAPI 我的主要问题是:如果我使用了一个非常大的文件,为什么我的web客户端要花这么长时间才能显示收到了响应?无论文件大小如何,我返回响应的方法似乎都能很快完成 下面是我的代码以及输出和两个不同的HTTPPOST语句及其结果。在我的第一个示例中,我使用了一个246KB的文件,并在20ms内从客户端获得响应。第二个示例使用一个2089344KB的文件,并在大约45秒后在客户端获得响应。两个日志都显示,从方法开始到创建结果之间只有几毫秒的时间 感谢您的帮助!谢谢 代码:C# 在C中使用RESTAPI时,什么会影响HTTP POST的响应时间#,c#,rest,C#,Rest,我正在尝试用C#后端制作一个简单的RESTWebAPI 我的主要问题是:如果我使用了一个非常大的文件,为什么我的web客户端要花这么长时间才能显示收到了响应?无论文件大小如何,我返回响应的方法似乎都能很快完成 下面是我的代码以及输出和两个不同的HTTPPOST语句及其结果。在我的第一个示例中,我使用了一个246KB的文件,并在20ms内从客户端获得响应。第二个示例使用一个2089344KB的文件,并在大约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;
}
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
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
第一个HTTP帖子:
[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;
}
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
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
第一反应:
[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;
}
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
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
第一个日志:
[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;
}
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
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
第二个HTTP帖子:
[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;
}
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
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
第二反应:
[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;
}
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
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
第二个日志:
[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;
}
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
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();
至于为什么现在看不到时差,整个请求在调用控制器之前发送到服务器。请参阅此问题,了解流式处理请求的解决方案,而不是等待整个正文