Why can't I get a Web page charset from the HTTP protocol

Source: Internet
Author: User
Tags character set html page

The original question is as follows:

Http://topic.csdn.net/u/20080902/02/a6445aa1-2e6b-45c6-a47c-79009718c0fa.html

The contents of an HTML Web page are roughly as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type;charset=gb2312" />
<title>CSDN首页 </title>
...
<body>
.....

I use the following statement to crawl a page similar to the above:

URL url = new URL("http://www.csdn.net");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//建立连接后,使用下面两句取得网页的编码格式
String contentType = connection.getHeaderField("Content-Type");
contentType = connection.getContentType();

Either way, the value of the ContentType is text/html, not the back charset=gb2312, why?

I noticed a problem, content= "text/html; charset=gb2312 "Here, there is a space between and charset, whether it is because of this space, but not the exact value of Content-type

Is there any other way to get the coded character set of the page?

Reply:

Connection.getcontenttype ();

This requires the server to set up the ContentType, this data in the file header, rather than in the file body (that is, the HTML you see) inside.

So it takes 3 steps to detect the page type, and if that doesn't work, use the next

1 Look at the contenttype in the header, that's the way you used it.

2 look at the HTML inside the ContentType, that is, parsing HTML page

3 Automatic evaluation of HTML data, similar to the automatic code type in it.

Header data for a typical return message

http/1.1 OK

Date:mon, Sep 2008 23:13:31 GMT

server:apache/2.2.4 (WIN32) mod_jk/1.2.26

Vary:host,accept-encoding

set-cookie:java2000_style_id=1; Domain=www.java2000.net; Expires=thu, 03-nov-2011 09:00:10 GMT; path=/

Content-encoding:gzip

Transfer-encoding:chunked

Content-type:text/html;charset=utf-8

。。。。 Starting from here is the data ontology

This article is stored in: http://www.java2000.net/p9226

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.