c#使用HttpWebRequest建立大量内存

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

你好,我在运行基于大型请求的程序时遇到了httpwebrequest的问题,大量的内存堆积。可能发生的唯一真实的情况是在发送请求后将流读回,我将如何应对这一问题或更好地限制此类事情?

我也有各种(10-15)if语句在响应的主体中查找各种内容,但即将将它们更改为switch语句,尽管我怀疑它们会导致如此高的内存堆积,我认为这会最好提一下。我的请求看起来像这样:

HttpWebResponse response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


                request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
                request.Headers.Add("Upgrade-Insecure-Requests", @"1");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.88";


                response = (HttpWebResponse)request.GetResponse();

                string _responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();

            catch (WebException e)
            {

            }
            catch (UriFormatException p)
            {


            }

您可以看到我的流是通过_responseData读回的,然后我使用.contains来查看我要查找的内容是否存在。

c# out-of-memory
1个回答
0
投票
  • 将HttpWebRequest.AllowWriteStreamBuffering设置为false。
  • 处置HttpWebResponse,Stream和StreamReader。
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowWriteStreamBuffering = false;

                request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
                request.Headers.Add("Upgrade-Insecure-Requests", @"1");
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.88";

                string _responseData;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                        {
                            _responseData = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (WebException e)
            {

            }
            catch (UriFormatException p)
            {

            }
© www.soinside.com 2019 - 2024. All rights reserved.