MyException - 我的异常网
当前位置:我的异常网» ASP.NET » 怎的将页面转换为字符串

怎的将页面转换为字符串

www.MyException.Cn  网友分享于:2013-12-24  浏览:15次
怎样将页面转换为字符串
就是将页面的html代码转换出来!谢谢

------解决方案--------------------
// 获取指定网页的HTML代码
static string GetPageSource(string URL)
{
Uri uri = new Uri(URL);
HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse hwRes = (HttpWebResponse)hwReq.GetResponse();
hwReq.Method = "Get ";
hwReq.KeepAlive = false;
StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding( "UTF-8 "));
return reader.ReadToEnd();
}

注意下网页的编码,改下其中的GetEncoding( "UTF-8 "),否则可能出现乱码
------解决方案--------------------
比如:得到本站点下webform2.aspx运行后的html System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb); Server.Execute( "webform2.aspx ",sw); sw.Close(); Response.Write(Server.HtmlEncode(sb.ToString()));

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有