首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

认为自己是高手的进来看看这个有关问题?

2012-02-02 
认为自己是高手的进来看看这个问题??WebRequestmywebrqWebRequest.Create( http://www.h315.com/xwzx.as

认为自己是高手的进来看看这个问题??
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;


string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;
try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
sException = e.Message.ToString();

}
catch (Exception e)
{
sException = e.ToString();

}
finally
{
if (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(),Encoding.UTF8);
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
public static void CreateFileByPath(string filePath, string fileCode)
{
//如果文件已存在,则删除此文件
if (File.Exists(filePath))
{
File.Delete(filePath);
}
FileStream fs = new FileStream(filePath, FileMode.CreateNew);
StreamWriter w = new StreamWriter(fs,Encoding.GetEncoding( "gb2312 "));
w.Write(fileCode);
w.Close();
fs.Close();
}

热点排行