获取网页编码,为什么有的只返回“text/html”,而有的确返回完整的“text/html; charset=gb2312”

school 2007-02-20 03:57:42
代码如下:

String^ strWebPageAddress; // 网址
String^ strContentType;
Uri^ uri;
WebRequest^ wr;
Stream^ stm;
MessageBox^ msgbox;
for(int i = 0; i < this->dataGridView1->DisplayedRowCount(false) - 1; ++i)
{
strWebPageAddress = this->dataGridView1[this->pageAddressDataGridViewTextBoxColumn->Name, i]->Value->ToString();

uri = gcnew Uri( strWebPageAddress );
wr = WebRequest::Create( uri );
stm = wr->GetResponse()->GetResponseStream();

strContentType = wr->GetResponse()->Headers[ L"Content-Type" ];
msgbox->Show( strContentType );

stm->Close();
}

从数据库中获取以下5个网址代入strWebPageAddress进行测试:
http://www.baidu.com/
http://jxfc.online.jx.cn/
http://www.2ed.cn/ArticleList-156.shtml
http://www.jict.org/jictold/quality.htm
http://html.hjsm.tom.com/html/book/27/875/42d953464,0fa5.htm

msgbox显示strContentType中的返回内容按以上网址顺序分别为:
text/html
text/html
text/html; charset=gb2312
text/html
text/html; charset=utf8

用IE查看相关网页源代码顺序为
text/html; charset=gb2312
text/html; charset=gb2312
text/html; charset=gb2312
text/html; charset=gb2312
text/html; charset=utf8

为什么无法返回完整的信息???如何解决这个问题???
...全文
706 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jumbogu 2009-01-20
  • 打赏
  • 举报
回复
To sum up, conforming user agents must observe the following priorities when determining a document's character encoding (from highest priority to lowest):

An HTTP "charset" parameter in a "Content-Type" field.
A META declaration with "http-equiv" set to "Content-Type" and a value set for "charset".
The charset attribute set on an element that designates an external resource.
In addition to this list of priorities, the user agent may use heuristics and user settings. For example, many user agents use a heuristic to distinguish the various encodings used for Japanese text. Also, user agents typically have a user-definable, local default character encoding which they apply in the absence of other indicators.

User agents may provide a mechanism that allows users to override incorrect "charset" information. However, if a user agent offers such a mechanism, it should only offer it for browsing and not for editing, to avoid the creation of Web pages marked with an incorrect "charset" parameter.

蒋晟 2007-02-22
  • 打赏
  • 举报
回复
http头和网页指定的编码可能不一致的
浏览器一般以网页内容为准
Juchiyufei 2007-02-22
  • 打赏
  • 举报
回复
应该是解析出错吧,或者就是字符编码不对引起的解析时出错.
school 2007-02-20
  • 打赏
  • 举报
回复
问题是测试的5个网页的源码中的meta标记如下:
<meta http-equiv=Content-Type content="text/html;charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

为什么会返回
text/html
text/html
text/html; charset=gb2312
text/html
text/html; charset=utf8

?????????????????
第1,2,4的网页只返回了“text/html”,而丢失了“charset=gb2312”????!!!
lb_hit 2007-02-20
  • 打赏
  • 举报
回复
HTTP里的Content-Type字段是否指出页面的编码字符集是网页服务器决定的,“text/html”或者“text/html; charset=gb2312”都是正常的。要获得网页确切的编码方式需要解析HTML文件,HTTP头部的meta字段通常会指出。
例如:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

更多关于HTML的定义可以参见W3C的文档:
http://www.w3.org/TR/1998/REC-html40-19980424/

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧