Java URL Connection getCharset(URLConnection connection)

Here you can find the source of getCharset(URLConnection connection)

Description

get Charset

License

Open Source License

Declaration

static String getCharset(URLConnection connection) 

Method Source Code

//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;
    }
}

Related

  1. getAsStream(URL url)
  2. getBaseAuthInputStreamFromURL(String query, String basicAuthString)
  3. getBaseURL(URLConnection conn)
  4. getBooleanFromUrl(String url)
  5. getBytes(String urlStr)
  6. getCharsFromURL(URL url)
  7. getChildren(URL url)
  8. getConnection(URL url)
  9. getConnectionResponseHeaders(URLConnection c)