C#网页下载乱码

来源:百度知道 编辑:UC知道 时间:2024/06/06 16:50:34
我写了个下载网页并在本地打开的程序,但中文网页会显示为乱码,各位大侠有知道怎么解决的么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace NetConsole_CSharp
{
class Program
{
static void Main(string[] args)
{
string s;
string head = "http://";
s = Console.In.ReadLine();
s = head + s;
Uri uri = new Uri(s);
WebClient client = new WebClient();
client.BaseAddress = head + uri.Authority;
string data = client.DownloadString(uri.AbsolutePath);
Encoder e = Encoding.ASCII.GetEncoder();
Char[] chars = data.ToCharArray();
int byteCount = e.GetByteCount(chars, 0, chars.Length, true);
Byte[] bytes = new Byte[byteCount];
int bytesEncodedCount = e.GetByte

System.File.WriteAllText("c:/temp.htm", new System.Net.WebClient().DownLoadString(uri, System.Text.Encoding.GetEncoding("网页编码")));
System.Diagnostics.Process.Start("temp.htm");

编码问题,中文网页一般用gb2312或utf8,智能点的也可以写个方法检测编码

Encoder e = Encoding.ASCII .GetEncoder();改为 Encoder e = Encoding.UTF8 .GetEncoder();好像就好了

System.Net;

WebClient.DownloadFileAsync;

下载到本地(用记事本打开是不会乱码的),然后再解码。

有个读取文件的类,不管是什么编码都可以读取的,你找找看。