(有没有人可以帮我啊!)关于使用httpwebrequest抓网页用streamreader.readtoend()读网页内容时有些网页会导致cpu100%并死机?
在使用httpwebrequest抓网页,然后将得到的流用streamreader.readtoend方法读出时,有些网页在执行这一方法前都正常,但一执行这一方法时就会导致cpu100%,并再也动不了了。找了2天了,都没有找到解决办法。有没有人可以帮我啊!代码如下:
public string GetHtmlPageSource(string strUrl, int inTimeOut, string PageEnCode)
{
if (strUrl.ToLower().StartsWith("http://"))
{
string strSource = "";
HttpWebRequest objRequest = null;
HttpWebResponse objResponse = null;
Stream objStream = null;
StreamReader objReader = null;
objRequest = (HttpWebRequest)WebRequest.Create(strUrl);
objRequest.Method = "GET";
objRequest.AllowAutoRedirect = true;
objRequest.MaximumAutomaticRedirections = 3;
//objRequest.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.2;.NET CLR 1.1.4322)";
objRequest.Referer = objRequest.RequestUri.ToString();
objRequest.Timeout = 15 * 1000;
objRequest.KeepAlive = true;
try
{
#region 处理过程
//objRequest = (HttpWebRequest)WebRequest.Create(strUrl);
//objRequest.Method = "GET";
//objRequest.AllowAutoRedirect = true;
//objRequest.Timeout = inTimeOut * 1000;
//objRequest.KeepAlive = false;
objResponse = (HttpWebResponse)objRequest.GetResponse();
//2008-10-10 23:17
if (objResponse != null)
{
//<<
//2008-10-11 13:24
//int intBytes = objResponse.ContentLength;
//<<
PageEnCode = PageEnCode.Replace("“", "").Replace("”", "");
if (PageEnCode.Trim() == "")
{
PageEnCode = "gb2312";
}
objStream = objResponse.GetResponseStream();
objStream.ReadTimeout = 15 * 1000; //2008-10-4 1958
objStream.WriteTimeout = 15 * 1000;
//(原)StreamReader objReader = new StreamReader(objStream, Encoding.GetEncoding(PageEnCode.Trim()));
objReader = new StreamReader(objStream, Encoding.GetEncoding(PageEnCode.Trim()));
//StreamReader objReader = new StreamReader(objStream, Encoding.GetEncoding("gb2312"));
//try
//{
strSource = objReader.ReadToEnd(); //!!!那些导致cpu100%的问题就出现在执行了这一步后。!!!!
objReader.Dispose();
objResponse.Close();
objStream.Close();
return strSource;
//}
//catch (IOException ex)
//{
// return "写错误";
//}
//finally
//{
// if (objResponse != null)
// {
// objResponse.Close();
// }
// objReader.Dispose();
// objStream.Close();
//}
//2008-10-10 23:17
}
else
{
objResponse.Close();
return "";
}
//<<
#endregion
}
catch (System.Net.WebException webEx)
{
return "E-" + webEx.Status.ToString().ToUpper();
}
catch (IOException ex)
{
return "主机断开连接";
}
finally
{
if (objReader != null) { objReader.Close(); }
if (objStream != null) { objStream.Close(); }
if (objResponse != null) { objResponse.Close(); }
if (objRequest != null) { objRequest = null; }
}
}
else
{
return "E-指定的网址格式 不正确";
}
} --------------------编程问答--------------------
补充:.NET技术 , C#