日期:2014-05-19  浏览次数:20432 次

认为自己是高手的进来看看这个问题??
WebRequest   mywebrq   =   WebRequest.Create( "http://www.h315.com/xwzx.aspx ");
WebResponse   mywebresp   =   mywebrq.GetResponse();

Encoding   code   =   Encoding.GetEncoding( "utf-8 ");
StreamReader   sr   =   new   StreamReader(mywebresp.GetResponseStream(),code);
string   strHtml=sr.ReadToEnd();
StreamWriter   sw=File.CreateText(Server.MapPath( "index.htm "));
sw.Write(strHtml);
sw.Close();
Response.WriteFile(Server.MapPath( "index.htm "));}

这段代码能够正常显示网页,但保存下来的网页就是乱码,我把字符编码换成gb2312和换成default也不行!哪个大侠能帮俺看看??万分感谢了!!


------解决方案--------------------
WebRequest mywebrq = WebRequest.Create( "http://www.h315.com/xwzx.aspx ");
WebResponse mywebresp = mywebrq.GetResponse();
StringBuilder sb=new StringBuilder();

Encoding code = Encoding.GetEncoding( "utf-8 ");
StreamReader sr = new StreamReader(mywebresp.GetResponseStream(),code);
string strHtml=sr.ReadToEnd();

// StreamWriter sw=new File.CreateText(Server.MapPath( "index.htm "));
StreamWriter sw=new StreamWriter(Server.MapPath( "index.htm "),false,Encoding.UTF8);

sw.Write(strHtml);
sw.Close();
Response.WriteFile(Server.MapPath( "index.htm "));
------解决方案--------------------
临时解决的方案 你可以用xmlhttp获取 代替WebRequest
将下面的文本框 里面的数据 保存为index.htm即可


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> 网站首页静态生成文件 </title>
<script language= "javascript ">
function getXML(URL)
{
var xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP ");
xmlhttp.Open( "GET ",URL, false);
try {
xmlhttp.send();
var result = xmlhttp.status;}
catch(e) {return(false);}
if(result==200) {
return(xmlhttp.responseText);
}
delete(xmlhttp)
}
function setData()
{
try
{
document.getElementById( "tbhome ").innerText = getXML( 'http://www.h315.com/xwzx.aspx ');//+ '?para= '+Math.random());
}
catch(e)
{
alert( '在获取数据时出错! ');
}
}
</script>
</HEAD>
<body onload= "setData(); ">
<br>
<p> <br>
</p>
<form id= "frmHome " method= "post ">
<div align= "center ">
<textarea name= "tbhome " rows= "25 " cols= "80 " id= "tbhome "> </textarea>
</div>
</form>
</body>
</HTML>


------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
string filecode = GetHttpData( "http://www.h315.com/xwzx.aspx ");
CreateFileByPath(Server.MapPath( "index.htm "), filecode);
}
/// <summary>
/// 取得源网页代码
/// </summary>
/// <param name= "Url "> URL </param>
/// <returns> 返回HTML </returns>
public string GetHttpData(string Url)
{
string sException = null;