用gb2312编码这个页面却读不出来:http://www.firsoft.com/guestbook, 而非得用utf-8.
用utf-8的话很多其它网页又读不出来,如:http://job.inhe.net/Center/SearchCenter/JobList.php?ID=1像这种情况怎么办?这是我的代码段:
WebRequest request = WebRequest.Create( url.Text );
request.Timeout = Configuration.WebRequestTimeout * 1000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();Encoding encoding = System.Text.Encoding.GetEncoding("gb2312"); // encoding of gb2312
if (response.ContentEncoding.Length > 0)
{
encoding = Encoding.GetEncoding(response.ContentEncoding);
}
reader = new StreamReader(stream, encoding);
text = reader.ReadToEnd();
用utf-8的话很多其它网页又读不出来,如:http://job.inhe.net/Center/SearchCenter/JobList.php?ID=1像这种情况怎么办?这是我的代码段:
WebRequest request = WebRequest.Create( url.Text );
request.Timeout = Configuration.WebRequestTimeout * 1000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();Encoding encoding = System.Text.Encoding.GetEncoding("gb2312"); // encoding of gb2312
if (response.ContentEncoding.Length > 0)
{
encoding = Encoding.GetEncoding(response.ContentEncoding);
}
reader = new StreamReader(stream, encoding);
text = reader.ReadToEnd();
解决方案 »
- object相等的问题
- 散分,求一本C#深层次的书
- 这句Java代码怎么转换成C#代码,关键在于new byte[]中的负数元素
- 用c#.net做了一个操作远程sql服务器上上的课户端软件,请问连接服务器数据库上的代码写到课户端软件里可以吗?安全方面有问题吗?
- 怎样让一个按钮变为灰色,就是禁用状态?
- ------------------------一个弱智的问题...困扰了我..."Textbox"的问题-----------------------
- 这个编译错误时什么意思?
- 未将项目配置为进行调试?
- 毕业,便宜卖自己
- Bitmap map =new Bitmap()中如何控制大小,我想使所有的图片都在picturebox控件上按照picturebox的大小显示完整!
- 有没有关闭整个程序的代码啊? 在先线等待
- long.parse 能否将16进制字符串转化为数字? 16进制字符串是否写为 0xFFFF? 2进制呢?
<globalization
requestEncoding="gb2312"
responseEncoding="gb2312"
fileEncoding="gb2312"
/>
试试
yezie(椰子.Net): gb2312 已经试过了, 写在配制文件里应该是一样的.
在页面上最顶的Page指令可以指定响应编码。如果需要全站点,则直接写进webconfig里。如yezie(椰子.Net)所说。楼主说已经试过,不知道你是否真的试过而且并未写错,且页面已更新浏览。那方法应该是对的。
我是WINDOWS桌面程序, 是一个网页内容采集器, 呵呵, 对不起, 开始没说清!!用gb2312编码这个页面却读不出来:http://www.firsoft.com/guestbook, 而非得用utf-8.
用utf-8的话很多其它网页又读不出来,如:http://job.inhe.net/Center/SearchCenter/JobList.php?ID=1像这种情况怎么办?
http://blog.sunmast.com/natas/archive/2004/10/30/989.aspx
private static String DecodeData(WebResponse w)
{ //
// first see if content length header has charset = calue
//
String charset = null;
String ctype = w.Headers["content-type"];
if(ctype != null)
{
int ind = ctype.IndexOf("charset=");
if(ind != -1)
{
charset = ctype.Substring(ind + 8);
Console.WriteLine("CT: charset=" + charset);
}
} // save data to a memorystream
MemoryStream rawdata = new MemoryStream();
byte [] buffer = new byte[1024];
Stream rs = w.GetResponseStream();
int read = rs.Read(buffer,0,buffer.Length);
while(read > 0)
{
rawdata.Write(buffer,0,read);
read = rs.Read(buffer,0,buffer.Length);
} rs.Close(); //
// if ContentType is null, or did not contain charset, we search in body
//
if(charset == null)
{
MemoryStream ms = rawdata;
ms.Seek(0,SeekOrigin.Begin); StreamReader srr = new StreamReader(ms,Encoding.ASCII);
String meta = srr.ReadToEnd(); if(meta != null)
{
int start_ind = meta.IndexOf("charset=");
int end_ind = -1;
if(start_ind != -1)
{
end_ind = meta.IndexOf("\"", start_ind);
if(end_ind != -1)
{
int start = start_ind + 8;
charset = meta.Substring(start, end_ind - start + 1);
charset = charset.TrimEnd(new Char[] { '>','"' });
Console.WriteLine("META: charset=" + charset);
}
}
}
} Encoding e = null;
if(charset == null)
{
e = Encoding.ASCII; //default encoding
}
else
{
try
{
e = Encoding.GetEncoding(charset);
}
catch(Exception ee)
{
Console.WriteLine("Exception: GetEncoding: " + charset);
Console.WriteLine(ee.ToString());
e = Encoding.ASCII;
}
} rawdata.Seek(0,SeekOrigin.Begin); StreamReader sr = new StreamReader(rawdata, e); String s = sr.ReadToEnd(); return s.ToLower();
}