【发布时间】:2013-06-02 17:47:27
【问题描述】:
这是我的课:
public class HttpRequestHelper
{
public static string GetRequestText(string url, string method, string referer, string postData, int timeout = 50000, string userAgent = null, string proxy = null)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
request.Method = method;
request.Timeout = timeout;
if (!string.IsNullOrEmpty(proxy))
request.Proxy = new System.Net.WebProxy(proxy);
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
if (!userAgent.IsNullOrEmptyText())
{
request.UserAgent = userAgent;
}
if (!string.IsNullOrEmpty(referer))
{
request.Referer = referer;
}
if (method == "POST")
{
if (!string.IsNullOrEmpty(postData))
{
request.ContentLength = postData.Length;
using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}
}
else request.ContentLength = 0;
}
string result = string.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
}
我的网站通常占用大约 170mb 内存(数据库中大约有 40k 条记录)。但是当我调用GetRequestText 时,内存最高可达 1Gb。我不知道为什么?对此有何想法或解决方案?
提前感谢!
【问题讨论】:
-
那么
postData有多大,响应有多大? -
您好乔恩,感谢您的关注。无更新数据。我使用 GET 方法。响应是纯文本(html源代码)。
-
在这种情况下,您发布的大部分代码都是无关紧要的。如果您只包含 relevant 代码会很有帮助。好的,那么反应有多大?
-
我收到了来自 whatismyipaddress.com 的 GET 请求,格式为:whatismyipaddress.com/ip/[ipaddress]
-
好吧,我们仍然不知道导致问题的响应有多大 - 或者你这样做了多少次......如果你正在调用这个方法非常 i> 频繁(每秒数千次),那么也许这就是问题所在。内存增长的速度有多快?