易截截图软件、单文件、免安装、纯绿色、仅160KB

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导出Excel乱码的解决方案

在asp.net中导出excel 中比较通行的做法是: Response.ContentType = "application/vnd.ms-excel";
然后直接向里面扔 html 的table
但是有中文的时候 老出现乱码,有很多解决方案,但都不能通盘解决, 就是在 输出html两头输出
 
Response.Write("<html><head><meta http-equiv=Content-Type conte ......

asp.net 动态treeview

js code //主要功能是实现复选框的全选择跟非全选
<script type="text/javascript" defer="defer">
        function OnTreeNodeChecked() {
            var ele = event.srcElement;
    &nbs ......

asp.net的一个dbhelper类

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
namespace Maticsoft.DBUtility
{
     /// <summary>
  ......

asp.net平台上连接数据库

 今天纠结了一上午的问题,于下午3点12分尘埃落定!
事情是这样的:
作为一个里程碑记录下吧,也算是我第一次将asp.net与数据库结合,并完成从软件编程到web的过渡。
在此感谢今天为我解决问题的“杀手”(也称老道),还有以前为我解决问题的大队、御风、华哥等牛...
言归正传,本文介绍一下怎样在asp. ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号