ASP.NET 判断网页编码读取内容,防止乱码
最近要写一个在网页中查找关键字及链接的程序,在输出到TextBox的时候发现经常出现乱码,整理了一下根据不同的编码选取网页源文件,目前可以解决几种常编码方式的网页,感兴趣的可以试下。
本来想用“==”判断编码,感觉比较麻烦,所以改用比较模糊的方法,Contains用在这里挺方便的。
Contains说明:就是返回一布尔值,指示是否当前字符串实例包含给定的子串。
//存放网页的源文件
string all_code = "";
HttpWebRequest all_codeRequest = (HttpWebRequest)WebRequest.Create(web_url);
WebResponse all_codeResponse = all_codeRequest.GetResponse();
string contenttype = all_codeResponse.Headers["Content-Type"];//得到结果为"text/html; charset=utf-8"
//网页编码判断
if (contenttype.Contains("GB2312"))
{
StreamReader the_Reader = new StreamReader(all_codeResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//获取源文件
all_code = the_Reader.ReadToEnd();
the_Reader.Close();
}
else if (contenttype.Contains("GBK"))
{
StreamReader the_Reader = new StreamReader(all_codeResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
//获取源文件
all_code = the_Reader.ReadToEnd();
the_Reader.Close();
}
else if (contenttype.Contains("UTF-8"))
{
StreamReader the_Reader = new StreamReader(all_codeResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
//获取源文件
all_code = the_Reader.ReadToEnd();
the_Reader.Close();
}
相关文档:
ASP.NET 中的设计模式之MVC篇
ASP.NET
中的设计模式之
MVC
篇
设计模式
MVC
页面控制器
模板与
Page
基类
设计模式
软件开发中,软件复用和团队协作都一直是最为人们关注的重要问题之一。有趣的是,这两个似乎属于软件工程范畴的问题都有一个共同的技术方面的解决之道:设计模式。
  ......
One of my bank customers planned to roll out a new ASP.NET web application serving as their critical internet banking portal. They faced some weird performance issue in their UAT environment. They noticed the CPU utilization was really high (above 90%) without any fluctuation. After checking ......
之前要完成一个支持多支持多种格式的视频播放器,在网上找到一个VB写的,然后改成C#,近段在网上也见有不少人问这个问,在此分享
public class VideoPlayer
{
/// <summary>
/// 生成视频播放器的HTM ......
ASP.NET 首页性能的十大做法
前言
本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分:
1.采用 HTTP Module 控制页面的生命周期。
2.自定义Response.Filter得到输出流stream生成动态页面的静态内容(磁盘缓存)。
3.页面GZIP压缩。
4.OutputCache 编程方式输出页面缓存。
5.删除页面空白字符串 ......
本文介绍ASP.NET错误处理,以及介绍如果您的应用程序试图登录数据库时没有成功,则显示的错误信息不应该包括它正在使用的用户名。
要创建页中的全局处理程序,请创建 Page_Error 事件的处理程序。要创建ASP.NET应用程序范围的错误处理程序,请在 Global.asax 文件中将代码添加到 Application_Error 方法。只要您的页或应 ......