我已经浪费了2天的时间才发现,WebBrowser控件中存在已知的内存泄漏(自2007年左右以来,他们还没有修复它)所以我决定在这里问一下,该怎么做我需要。
直到现在,(使用WebBrowser ...),我一直在访问一个网站,(ctrl + a),将其粘贴到一个字符串,这就是全部。我的字符串中有网页的文本内容。完美地工作直到我发现它需要1 gb的内存一段时间后。 是否可以通过HttpWebRequest,httpwebclient或其他任何方式来实现?
感谢您的回复,没有任何类似的帖子(或者我没有找到任何帖子,搜索并没有真正吸引我,因为我现在非常生气:P)
FORGOT要添加: 我不想要HTML代码,我知道可以轻松搞定。就我而言,HTML代码是无用的。我确实需要文本用户在使用互联网浏览器打开页面时看到。
答案 0 :(得分:7)
using (WebClient client = new WebClient())
{
string html = client.DownloadString("http://stackoverflow.com/questions/10839877/how-to-get-a-txt-content-of-a-web-page");
}
答案 1 :(得分:2)
您可以使用:
string getHtml(string url) {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader source = new StreamReader(myWebResponse.GetResponseStream());
string pageSourceStr = string.Empty;
pageSourceStr= source.ReadToEnd();
response.Close();
return pageSourceStr;
}
您仍然需要进行一些子字符串替换以将其从html减少为文本。如果你只想要来自某个div的文本,那也不算太糟糕。
答案 2 :(得分:2)
这将从任何网页下载html内容。
WebClient client = new WebClient ();
string reply = client.DownloadString ("http://www.google.com");
答案 3 :(得分:1)