HttpWebRequest wwb = (HttpWebRequest)WebRequest.Create(url);
wwb.UserAgent = "Mozilla/5.0";//用户代理;与浏览器相关;没有的话,可能会导致GetResponse()
wwb.ContentType = "text/html; charset=UTF-8";
wwb.Method = "GET";
WebResponse wwebRes = null;// (HttpWebResponse)wwb.GetResponse();
Stream wwebStream = null;
StreamReader sssr = null;// new StreamReader(wwebRes.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
//string sstrHtml;
StringBuilder sb = new StringBuilder();
try
{
wwebRes = (HttpWebResponse)wwb.GetResponse();
}
catch (WebException ex)
{
//wwebRes = (HttpWebResponse)ex.Response;//错误信息
return "";
}
finally
{
//及时释放资源
if (wwebRes != null)
{
wwebStream = wwebRes.GetResponseStream();
sssr = new StreamReader(wwebRes.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
//获得网页源码
string str1 = null;
while ((str1 = sssr.ReadLine()) != null)
{
sb.Append(str1);
}
// sstrHtml = sssr.ReadToEnd();
wwebStream.Close();
sssr.Close();
wwebRes.Close();
wwb.Abort();
}
}