C#应用正则表达式将相当路径转化为绝对路径
2010-06-22 21:10
423 查看
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Text.RegularExpressions; public partial class Default7 : System.Web.UI.Page { // C#应用正则表达式将相当路径转化为绝对路径 //(来源:http://code.nontalk.com/2007/04/convert-relative-paths-to-absolute.html) public static String ConvertRelativePathsToAbsolute(String text, String absoluteUrl) { String value = Regex.Replace(text,"<(.*?)(src|href)=\"(?!http)(.*?)\"(.*?)>","<$1$2=\"" + absoluteUrl + "$3\"$4>", RegexOptions.IgnoreCase | RegexOptions.Multiline); // Now just make sure that there isn't a // because if // the original relative path started with a / then the // replacement above would create a //. return value.Replace(absoluteUrl + "/", absoluteUrl); } protected void Page_Load(object sender, EventArgs e) { //简单例子 string url = "http://blog.csdn.net/gdjlc/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 20000; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8)) { if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024) { string html = reader.ReadToEnd(); html = ConvertRelativePathsToAbsolute(html, "http://blog.csdn.net/"); Response.Write(html); } } } } }
相关文章推荐
- C#应用正则表达式将相当路径转化为绝对路径
- C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别
- C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别
- c#求出占用本地端口的程序的应用文件的绝对路径
- C# ,asp.net 获取当前,相对,绝对路径
- Tomcat下获取web应用的绝对路径
- 相对路径转化成绝对路径
- Silverlight.XNA(C#)跨平台3D游戏研发手记:(二)四叉树遍历与人工智能A*算法在SLG移动路径范围测算中的应用
- asp.net(C#)服务器绝对路径转换成URL相对路径[修改] (转)
- 关于C#中相对路径和绝对路径
- C# 将绝对路径转换为相对路径
- Weblogic web应用中获取文件的绝对路径
- 【原创源码】(05):C#-计算绝对路径。
- C# url 路径转换 相对路径 转换为 绝对路径
- C#、ASP.NET获取当前应用程序的绝对路径,获取程序工作路径
- C# 获取当前应用程序的绝对路径支持asp.net
- mfc 获得应用系统运行时的绝对路径
- 写一个函数怎么把相对路径转化为绝对路径(还是绝对路径转化为相对路径)?
- C# WinFrom 文件绝对路径
- Silverlight.XNA(C#)跨平台3D游戏研发手记:(二)四叉树遍历与人工智能A*算法在SLG移动路径范围测算中的应用