如何在.NET 4.6.1 + ASP.NET Web API 2中正确使用HttpClient

问题描述 投票:0回答:1

我正在使用机器翻译来创建这篇文章。请原谅任何语法错误。

我正在维护在 .NET 4.6.1 上运行的 ASP.NET Web API 2。

(别提支持结束了!我已经告诉他们升级很多次了。我那没有同情心的老板有祸了!!)

在此应用程序中,我正在考虑使用

HttpClient
访问其他网络服务器。

这是我写的代码:

namespace myApps.Controllers.WebAPI
{
    public class TempTestController : ApiController
    {
        private static HttpClient httpClient = new HttpClient();

        [Route("AthorSiteContent")]
        public IHttpActionResult getAthorSiteContent(string A_few_URLs)
        {
            var firstRequest = new System.Net.Http.HttpRequestMessage();
            firstRequest.Method = System.Net.Http.HttpMethod.Head;
            firstRequest.RequestUri = new Uri(A_few_URLs);

            var firstResponse = httpClient.SendAsync(firstRequest).GetAwaiter().GetResult();

            // check headers(host, status, Content-Type, Content-Length, etc...)
            // If the content cannot be downloaded, the process ends here.

            var secondRequest = new System.Net.Http.HttpRequestMessage();
            secondRequest.Method = System.Net.Http.HttpMethod.Get;
            secondRequest.RequestUri = new Uri(A_few_URLs);

            var secondResponse = httpClient.SendAsync(secondRequest).GetAwaiter().GetResult();

            var contentData = secondResponse.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();

            // Save contentData to a file

            return new ResponseMessageResult(this.Request.CreateResponse(HttpStatusCode.NoContent));
        }
    }
}

此代码安全吗?不会导致套接字耗尽吗?

此外,我没有使用

HttpWebRequest
,因为我发现信息表明在ASP.NET中使用HttpWebRequest会导致套接字耗尽。

c# sockets asp.net-web-api2 httpclient .net-4.6.1
1个回答
0
投票

当前代码

can
导致套接字耗尽,为什么?因为对于每个请求,您都使用两个不同的
HttpClientHandler
,但至少您可以使其更安全。

假设这是请求的 URL:

https://cdn.example.com/files/today.mp4

我认为最好像这样使代码更安全:

public class TempTestController : ApiController
    {
        //private static HttpClient httpClient = new HttpClient(); this won't help without baseAddress

        [Route("AthorSiteContent")]
        public async Task<IHttpActionResult> getAthorSiteContent(string A_few_URLs)
        {
            // url: https://cdn.example.com/files/today.mp4
            // baseAddress: https://cdn.example.com
            var HttpClient httpClient = new HttpClient()
            {
                BaseAddress= new Uri("https://cdn.example.com")
            };
            
            {
                var firstRequest = new System.Net.Http.HttpRequestMessage();
                firstRequest.Method = System.Net.Http.HttpMethod.Head;
                firstRequest.RequestUri = new Uri("/files/today.mp4"); //using the same httpClientHandler
                var firstResponse = await httpClient.SendAsync(firstRequest);

                //check headers(host, status, Content-Type, Content-Length, etc...)
                //If the content cannot be downloaded, the process ends here.
            }

            {
                var secondRequest = new System.Net.Http.HttpRequestMessage();
                secondRequest.Method = System.Net.Http.HttpMethod.Get;
                secondRequest.RequestUri = new Uri("/files/today.mp4"); //using the same httpClientHandler
                var secondResponse = await httpClient.SendAsync(secondRequest);
                var contentData = await secondResponse.Content.ReadAsByteArrayAsync();

                //Save contentData to a file
            }

            return new ResponseMessageResult(this.Request.CreateResponse(HttpStatusCode.NoContent));
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.