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 中比较通行的做法是: Response.ContentType = "application/vnd.ms-excel";
然后直接向里面扔 html 的table
但是有中文的时候 老出现乱码,有很多解决方案,但都不能通盘解决, 就是在 输出html两头输出
Response.Write("<html><head><meta http-equiv=Content-Type conte ......
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 ......
Web 页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留。
0、引言
Web 页面是无状态的,服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留。在用Asp.NET 设计开发一个Web ......
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 ......