Here you can find the source of getCharset(URLConnection connection)
static String getCharset(URLConnection connection)
//package com.java2s; /*/*from w w w . j a v a2s.c o m*/ * JScripter Simulation 1.0 - For Java To Script * Copyright (C) 2008-2011 J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.net.URLConnection; import java.util.StringTokenizer; public class Main { static String getCharset(URLConnection connection) { String contentType = connection == null ? null : connection.getContentType(); if (contentType != null) { StringTokenizer tok = new StringTokenizer(contentType, ";"); if (tok.hasMoreTokens()) { tok.nextToken(); while (tok.hasMoreTokens()) { String assignment = tok.nextToken().trim(); int eqIdx = assignment.indexOf('='); if (eqIdx != -1) { String varName = assignment.substring(0, eqIdx).trim(); if ("charset".equalsIgnoreCase(varName)) { String varValue = assignment.substring(eqIdx + 1); return unquote(varValue.trim()); } } } } } return null; } static String unquote(String text) { if (text.startsWith("\"") && text.endsWith("\"")) { return text.substring(1, text.length() - 2); } return text; } }