try something like string s = ".............."//if the url starts with "http://", do not replace //for relative path
string sURL = "http://www.microsoft.com/aaa/";
s = Regex.Replace(s,
@"<a\s+href\s*=\s*(['""])(?!http://|/)([^>]+)\1([^>]*)>",
"<a href=$1" + sURL + "$2$1$3>",
RegexOptions.Singleline | RegexOptions.IgnoreCase); //for absolute path
sURL = "http://www.microsoft.com/";
s = Regex.Replace(s,
@"<a\s+href\s*=\s*(['""])(?!http://)/([^>]+)\1([^>]*)>",
"<a href=$1" + sURL + "$2$1$3>",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
string sURL = "http://www.microsoft.com/aaa/";
s = Regex.Replace(s,
@"<a\s+href\s*=\s*(['""])(?!http://|/)([^>]+)\1([^>]*)>",
"<a href=$1" + sURL + "$2$1$3>",
RegexOptions.Singleline | RegexOptions.IgnoreCase); //for absolute path
sURL = "http://www.microsoft.com/";
s = Regex.Replace(s,
@"<a\s+href\s*=\s*(['""])(?!http://)/([^>]+)\1([^>]*)>",
"<a href=$1" + sURL + "$2$1$3>",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;namespace Web访问
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(496, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(480, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "http://www.chinamp3.com/mainland/disclist.php?key=
崔健&singer_sex=1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 40);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(560, 224);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.textBox1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
textBox2.Text = Gethtml(textBox1.Text.ToString()); } private string Gethtml(string url)
{
string tmpurl = UrlEncode(url); try
{
HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create(tmpurl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Mozilla/4.0";
HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("gb2312"); //gb2312中文
编码
if (loWebResponse.ContentEncoding.Length > 0)
{
enc = Encoding.GetEncoding(loWebResponse.ContentEncoding);
}
StreamReader loResponseStream = new
StreamReader(loWebResponse.GetResponseStream(),enc);
string s = loResponseStream.ReadToEnd();
loResponseStream.Close();
loWebResponse.Close();
//return tmphtml;
//string Dir = System.Text.RegularExpressions.Regex.Match(url,@"^.+/"); //string sURL = "http://www.chinamp3.com/mainland/";
//s = Regex.Replace(s,
@"<a\s+href\s*=\s*(['""])(?!http://)/([^>]+)\1([^>]*)>", "<a href=$1" + sURL
+ "$2$1$3>",
// RegexOptions.Singleline | RegexOptions.IgnoreCase);
//return "<base href=" +
System.Text.RegularExpressions.Regex.Match(url,@"^.+/")+ ">"+tmphtml;
return s;
}
catch(Exception)
{
//TODO : 处理错误
MessageBox.Show("html返回失败");
string error = "error";
return error;
}
} private string UrlEncode(string url)
{
byte[] bs=Encoding.GetEncoding("gb2312").GetBytes(url);
StringBuilder sb=new StringBuilder();
for(int i=0;i<bs.Length;i++)
{
if(bs[i]<128)
sb.Append((char)bs[i]);
else
{
sb.Append("%"+bs[i++].ToString("x").PadLeft(2,'0'));
sb.Append("%"+bs[i].ToString("x").PadLeft(2,'0'));
}
}
return sb.ToString();
}
}
}