读书人

为何不能生成静态页面

发布时间: 2011-12-25 23:21:20 作者: rapoo

为什么不能生成静态页面?
下面是我的方法,这个方法在本地可能将网上任何一个页面生成静态页面,
但是放在服务器上就生成不了,发现不能 getUrltoHtml(string Url)
方法得不到网页的Html代码

/// <summary>
/// 传入URL返回网页的html代码
/// </summary>
/// <param name= "Url "> URL </param>
/// <returns> </returns>
public string getUrltoHtml(string Url)
{
string errorMsg = " ";
string resultHtml = " ";
//try
//{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp = wReq.GetResponse();

// Get the response stream.
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream).Encoding.GetEncoding( "gb2312 ")
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.UTF8);
resultHtml= reader.ReadToEnd();
reader.Close();
reader.Dispose();

//}
//catch (System.Exception ex)
//{
// errorMsg = ex.Message;


//}
return resultHtml;
}
private const string FILE_NAME = "html.html ";
public void Main(string filePath,string fileUrl)
{
string path = HttpContext.Current.Server.MapPath(filePath);
System.Text.Encoding code = System.Text.Encoding.UTF8;

StreamWriter sw = new StreamWriter(path, false, code);

try
{
string str = this.getUrltoHtml(fileUrl);
//如果html为空则说明请求失败
if (str == " ")
{
this.lblIsHtml.Text = "获取html不成功 ";

}
//如果html为不空则说明请求成功
else
{ this.lblIsHtml.Text = "获取html成功 "; }
sw.Write(str );
sw.Flush();
this.hypNew.Text = filePath+ "即将展开新页面 ";
this.hypNew.NavigateUrl = filePath;
// Response.Write(this.getUrltoHtml(fileUrl));


//如果文件存在
string strFile = " ";
sw.Close();

if (File.Exists(path))
{

try
{
FileStream fs = new FileStream(path, FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() > = 0)
{
strFile = sr.ReadToEnd();
}

//如果文件内容为空说明写入失败
if (strFile == " ")
{
this.lblIsFile.Text = "写入文件失败 ";

}
//如果文件内容不为空则说明写入成功


else
{
this.lblIsFile.Text = "写入文件成功 ";

}

}
catch
{
Response.Write( "有错误 ");

}

}

//如果文件不存在
else
{
this.lblIsFile.Text = "写入文件失败 ";

}

}
catch (Exception ex)
{
this.lblResult.Text = "生成静态页面失败 ";
sw.Close();
}
//finally
//{
// sw.Close();


//}


}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string filePath = this.txtFilePath .Text.Trim();
string fileUrl = this.txtFileUrl.Text;
this.Main(filePath, fileUrl);

}

[解决办法]
没加HTTP?

读书人网 >asp.net

热点推荐