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
基类
设计模式
软件开发中,软件复用和团队协作都一直是最为人们关注的重要问题之一。有趣的是,这两个似乎属于软件工程范畴的问题都有一个共同的技术方面的解决之道:设计模式。
  ......
js code //主要功能是实现复选框的全选择跟非全选
<script type="text/javascript" defer="defer">
function OnTreeNodeChecked() {
var ele = event.srcElement;
&nbs ......
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>
  ......
//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Respo ......
作者: Stephen Walther
原文地址:http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/UserProfiles.asp
译
者:Tony Qu
概要:许多ASP.NET应用程序需要跨访问的用户属性
跟踪功能,在ASP.NET1.1中,我们只能人工实现这一功能。但如今,使用 ASP.NET 2.0的Profile对象,这个过程变得异
......