怎样将页面转换为字符串
就是将页面的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()));