java.net.HttpURLConnection#getContentEncoding ( )源码实例Demo

下面列出了java.net.HttpURLConnection#getContentEncoding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: BiliRoaming   文件: BiliRoamingApi.java
private static String getContent(String urlString) throws IOException {
    URL url = new URL(urlString);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Build", String.valueOf(BuildConfig.VERSION_CODE));
    connection.setConnectTimeout(4000);
    connection.connect();

    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = connection.getInputStream();
        String encoding = connection.getContentEncoding();
        return StreamUtils.getContent(inputStream, encoding);
    }
    return null;
}
 
源代码2 项目: airsonic   文件: InternetRadioService.java
/**
 * Retrieve playlist data from a given URL.
 *
 * @param url URL to the remote playlist
 * @param maxByteSize maximum size of the response, in bytes, or 0 if unlimited
 * @param maxRedirects maximum number of redirects, or 0 if unlimited
 * @return the remote playlist data
 */
protected SpecificPlaylist retrievePlaylist(URL url, long maxByteSize, int maxRedirects) throws IOException, PlaylistException {

    SpecificPlaylist playlist;
    HttpURLConnection urlConnection = connectToURLWithRedirects(url, maxRedirects);
    try (InputStream in = urlConnection.getInputStream()) {
        String contentEncoding = urlConnection.getContentEncoding();
        if (maxByteSize > 0) {
            playlist = SpecificPlaylistFactory.getInstance().readFrom(new BoundedInputStream(in, maxByteSize), contentEncoding);
        } else {
            playlist = SpecificPlaylistFactory.getInstance().readFrom(in, contentEncoding);
        }
    } finally {
        urlConnection.disconnect();
    }
    if (playlist == null) {
        throw new PlaylistFormatUnsupported("Unsupported playlist format " + url.toString());
    }
    return playlist;
}
 
源代码3 项目: azeroth   文件: HttpUtils.java
private static HttpResponseEntity getResponseAsResponseEntity(HttpURLConnection conn) throws IOException {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    String charset = getResponseCharset(conn.getContentType());
    InputStream es = conn.getErrorStream();

    responseEntity.setStatusCode(conn.getResponseCode());
    if (es == null) {
        String contentEncoding = conn.getContentEncoding();
        if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
            responseEntity.setBody(getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset));
        } else {
            responseEntity.setBody(getStreamAsString(conn.getInputStream(), charset));
        }
    } else {
        String msg = getStreamAsString(es, charset);
        if (StringUtils.isEmpty(msg)) {
            responseEntity.setBody(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else {
            responseEntity.setBody(msg);
        }
    }

    return responseEntity;
}
 
源代码4 项目: azeroth   文件: HttpUtils.java
protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
    String charset = getResponseCharset(conn.getContentType());
    InputStream es = conn.getErrorStream();
    if (es == null) {
        String contentEncoding = conn.getContentEncoding();
        if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
            return getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset);
        } else {
            return getStreamAsString(conn.getInputStream(), charset);
        }
    } else {
        String msg = getStreamAsString(es, charset);
        if (StringUtils.isEmpty(msg)) {
            throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else {
            throw new IOException(msg);
        }
    }
}
 
源代码5 项目: appinventor-extensions   文件: YandexTranslate.java
/**
 * This method reads from a stream based on the passed connection
 * @param connection the connection to read from
 * @return the contents of the stream
 * @throws IOException if it cannot read from the http connection
 */
private static String getResponseContent(HttpURLConnection connection) throws IOException {
  // Use the content encoding to convert bytes to characters.
  String encoding = connection.getContentEncoding();
  if (encoding == null) {
    encoding = "UTF-8";
  }
  InputStreamReader reader = new InputStreamReader(connection.getInputStream(), encoding);
  try {
    int contentLength = connection.getContentLength();
    StringBuilder sb = (contentLength != -1)
        ? new StringBuilder(contentLength)
        : new StringBuilder();
    char[] buf = new char[1024];
    int read;
    while ((read = reader.read(buf)) != -1) {
      sb.append(buf, 0, read);
    }
    return sb.toString();
  } finally {
    reader.close();
  }
}
 
源代码6 项目: appinventor-extensions   文件: Web.java
private static String getResponseContent(HttpURLConnection connection) throws IOException {
  // Use the content encoding to convert bytes to characters.
  String encoding = connection.getContentEncoding();
  if (encoding == null) {
    encoding = "UTF-8";
  }
  InputStreamReader reader = new InputStreamReader(getConnectionStream(connection), encoding);
  try {
    int contentLength = connection.getContentLength();
    StringBuilder sb = (contentLength != -1)
        ? new StringBuilder(contentLength)
        : new StringBuilder();
    char[] buf = new char[1024];
    int read;
    while ((read = reader.read(buf)) != -1) {
      sb.append(buf, 0, read);
    }
    return sb.toString();
  } finally {
    reader.close();
  }
}
 
源代码7 项目: jeesuite-libs   文件: HttpUtils.java
private static HttpResponseEntity getResponseAsResponseEntity(HttpURLConnection conn) throws IOException {
	HttpResponseEntity responseEntity = new HttpResponseEntity();
	String charset = getResponseCharset(conn.getContentType());
	InputStream es = conn.getErrorStream();
	
	responseEntity.setStatusCode(conn.getResponseCode());
	if (es == null) {
		String contentEncoding = conn.getContentEncoding();
		if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(contentEncoding)) {
			responseEntity.setBody(getStreamAsString(new GZIPInputStream(conn.getInputStream()), charset));
		} else {
			responseEntity.setBody(getStreamAsString(conn.getInputStream(), charset));
		}
	} else {
		String msg = getStreamAsString(es, charset);
		if (StringUtils.isEmpty(msg)) {
			responseEntity.setBody(conn.getResponseCode() + ":" + conn.getResponseMessage());
		} else {
			responseEntity.setBody(msg);
		}
	}
	
	return responseEntity;
}
 
源代码8 项目: SimplicityBrowser   文件: SuggestionProvider.java
private String getEncoding(HttpURLConnection connection) {
    String contentEncoding = connection.getContentEncoding();
    if (contentEncoding != null) {
        return contentEncoding;
    }

    String contentType = connection.getContentType();
    for (String value : contentType.split(";")) {
        value = value.trim();
        if (value.toLowerCase(Locale.US).startsWith("charset=")) {
            return value.substring(8);
        }
    }

    return mEncoding;
}
 
源代码9 项目: database   文件: NanoSparqlClient.java
/**
		 * Write the response body on stdout.
		 * 
		 * @param conn
		 *            The connection.
		 *            
		 * @throws Exception
		 */
		protected void showResults(final HttpURLConnection conn)
				throws Exception {

			final LineNumberReader r = new LineNumberReader(
					new InputStreamReader(conn.getInputStream(), conn
							.getContentEncoding() == null ? "ISO-8859-1" : conn
							.getContentEncoding()));
			try {
				String s;
				while ((s = r.readLine()) != null) {
					System.out.println(s);
				}
			} finally {
				r.close();
//				conn.disconnect();
			}

		}
 
源代码10 项目: xipki   文件: ScepClient.java
protected ScepHttpResponse parseResponse(HttpURLConnection conn) throws ScepClientException {
  Args.notNull(conn, "conn");

  try {
    InputStream inputstream = conn.getInputStream();
    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
      inputstream.close();
      throw new ScepClientException("bad response: " + conn.getResponseCode() + "    "
              + conn.getResponseMessage());
    }
    String contentType = conn.getContentType();
    int contentLength = conn.getContentLength();

    ScepHttpResponse resp = new ScepHttpResponse(contentType, contentLength, inputstream);
    String contentEncoding = conn.getContentEncoding();
    if (StringUtil.isNotBlank(contentEncoding)) {
      resp.setContentEncoding(contentEncoding);
    }
    return resp;
  } catch (IOException ex) {
    throw new ScepClientException(ex);
  }
}
 
源代码11 项目: JYTB   文件: HttpRequestSender.java
private Response send(HttpURLConnection connection) throws IOException {
    connection.connect();

    if (connection.getResponseCode() != 200) {
        throw new IOException("Bad response! Code: " + connection.getResponseCode());
    }

    Map<String, String> headers = new HashMap<>();
    for (String key : connection.getHeaderFields().keySet()) {
        headers.put(key, connection.getHeaderFields().get(key).get(0));
    }

    String body;

    try (InputStream inputStream = connection.getInputStream()) {

        String encoding = connection.getContentEncoding();
        encoding = encoding == null ? Constants.ENCODING : encoding;

        body = IOUtils.toString(inputStream, encoding);
    } catch (IOException e) {
        throw new IOException(e);
    }

    if (body == null) {
        throw new IOException("Unparseable response body! \n {" + body + "}");
    }

    return new Response(headers, body);
}
 
源代码12 项目: xnx3   文件: HttpsUtil.java
/**
 * 得到响应对象
 * @param urlConnection
 * @param content 网页内容
 * @return 响应对象
 * @throws IOException
 */ 
private HttpResponse makeContent(String urlString, HttpURLConnection urlConnection, String content) throws IOException { 
    HttpResponse httpResponser = new HttpResponse(); 
    try { 
        httpResponser.contentCollection = new Vector<String>(); 
        String ecod = urlConnection.getContentEncoding(); 
        if (ecod == null) 
            ecod = this.encode; 
        httpResponser.urlString = urlString; 
        this.cookies=urlConnection.getHeaderField("Set-Cookie");
        httpResponser.cookie=this.cookies;
        httpResponser.defaultPort = urlConnection.getURL().getDefaultPort(); 
        httpResponser.file = urlConnection.getURL().getFile(); 
        httpResponser.host = urlConnection.getURL().getHost(); 
        httpResponser.path = urlConnection.getURL().getPath(); 
        httpResponser.port = urlConnection.getURL().getPort(); 
        httpResponser.protocol = urlConnection.getURL().getProtocol(); 
        httpResponser.query = urlConnection.getURL().getQuery(); 
        httpResponser.ref = urlConnection.getURL().getRef(); 
        httpResponser.userInfo = urlConnection.getURL().getUserInfo(); 
        httpResponser.content = content;
        httpResponser.contentEncoding = ecod; 
        httpResponser.code = urlConnection.getResponseCode(); 
        httpResponser.message = urlConnection.getResponseMessage(); 
        httpResponser.contentType = urlConnection.getContentType(); 
        httpResponser.method = urlConnection.getRequestMethod(); 
        httpResponser.connectTimeout = urlConnection.getConnectTimeout(); 
        httpResponser.readTimeout = urlConnection.getReadTimeout(); 
        httpResponser.headerFields = urlConnection.getHeaderFields();
        return httpResponser; 
    } catch (IOException e) { 
        throw e; 
    } finally { 
        if (urlConnection != null) 
            urlConnection.disconnect(); 
    } 
}
 
源代码13 项目: twill   文件: ResourceReportClient.java
private InputStream getInputStream(HttpURLConnection urlConn) throws IOException {
  InputStream is = urlConn.getInputStream();
  String contentEncoding = urlConn.getContentEncoding();
  if (contentEncoding == null) {
    return is;
  }
  if ("gzip".equalsIgnoreCase(contentEncoding)) {
    return new GZIPInputStream(is);
  }
  if ("deflate".equalsIgnoreCase(contentEncoding)) {
    return new DeflaterInputStream(is);
  }
  // This should never happen
  throw new IOException("Unsupported content encoding " + contentEncoding);
}
 
源代码14 项目: cloudstack   文件: VmwareContext.java
private Charset getCharSetFromConnection(HttpURLConnection conn) {
    String charsetName = conn.getContentEncoding();
    Charset charset;
    try {
        charset = Charset.forName(charsetName);
    } catch (IllegalArgumentException e) {
        s_logger.warn("Illegal/unsupported/null charset name from connection. charsetname from connection is " + charsetName);
        charset = StringUtils.getPreferredCharset();
    }
    return charset;
}
 
源代码15 项目: metrics   文件: HttpRequester.java
/**
 * 得到响应对象
 *
 * @param urlConnection
 * @return 响应对象
 * @throws IOException
 */
private HttpRespons makeContent(String urlString,
        HttpURLConnection urlConnection) throws IOException {
    HttpRespons httpResponser = new HttpRespons();
    try {
        InputStream in = urlConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(in));
        httpResponser.contentCollection = new Vector<String>();
        StringBuffer temp = new StringBuffer();
        String line = bufferedReader.readLine();
        while (line != null) {
            httpResponser.contentCollection.add(line);
            temp.append(line).append("\r\n");
            line = bufferedReader.readLine();
        }
        bufferedReader.close();

        String ecod = urlConnection.getContentEncoding();
        if (ecod == null)
            ecod = this.defaultContentEncoding;

        httpResponser.urlString = urlString;

        httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
        httpResponser.file = urlConnection.getURL().getFile();
        httpResponser.host = urlConnection.getURL().getHost();
        httpResponser.path = urlConnection.getURL().getPath();
        httpResponser.port = urlConnection.getURL().getPort();
        httpResponser.protocol = urlConnection.getURL().getProtocol();
        httpResponser.query = urlConnection.getURL().getQuery();
        httpResponser.ref = urlConnection.getURL().getRef();
        httpResponser.userInfo = urlConnection.getURL().getUserInfo();

        httpResponser.content = new String(temp.toString().getBytes(), ecod);
        httpResponser.contentEncoding = ecod;
        httpResponser.code = urlConnection.getResponseCode();
        httpResponser.message = urlConnection.getResponseMessage();
        httpResponser.contentType = urlConnection.getContentType();
        httpResponser.method = urlConnection.getRequestMethod();
        httpResponser.connectTimeout = urlConnection.getConnectTimeout();
        httpResponser.readTimeout = urlConnection.getReadTimeout();

        return httpResponser;
    } catch (IOException e) {
        throw e;
    } finally {
        if (urlConnection != null)
            urlConnection.disconnect();
    }
}
 
源代码16 项目: xnx3   文件: HttpUtil.java
/**
 * 得到响应对象
 * @param urlConnection
 * @return 响应对象
 * @throws IOException
 */ 
private HttpResponse makeContent(String urlString, HttpURLConnection urlConnection) throws IOException { 
	urlConnection.setConnectTimeout(this.timeout);
	urlConnection.setReadTimeout(this.timeout);
    HttpResponse httpResponser = new HttpResponse(); 
    try { 
        InputStream in = urlConnection.getInputStream(); 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); 
        httpResponser.contentCollection = new Vector<String>(); 
        StringBuffer temp = new StringBuffer(); 
        String line = bufferedReader.readLine(); 
        while (line != null) { 
            httpResponser.contentCollection.add(line); 
            temp.append(line).append("\r\n"); 
            line = bufferedReader.readLine(); 
        } 
        bufferedReader.close(); 
        String ecod = urlConnection.getContentEncoding(); 
        if (ecod == null) 
            ecod = this.encode; 
        httpResponser.urlString = urlString; 
        //urlConnection.getHeaderField("Set-Cookie");获取到的COOKIES不全,会将JSESSIONID漏掉,故而采用此中方式
        if(this.cookies == null || this.cookies.equals("")){
        	if(urlConnection.getHeaderFields().get("Set-Cookie") != null){
        		List<String> listS = urlConnection.getHeaderFields().get("Set-Cookie");
        		String cookie = "";
            	if(listS != null){
                    for (int i = 0; i < listS.size(); i++) {
        				cookie = cookie + (cookie.equals("")? "":", ") + listS.get(i);
        			}
            	}else{
            		cookie = urlConnection.getHeaderField("Set-Cookie");
            	}
            	this.cookies=cookie;
            	httpResponser.cookie=this.cookies;
        	}
        }
        httpResponser.defaultPort = urlConnection.getURL().getDefaultPort(); 
        httpResponser.file = urlConnection.getURL().getFile(); 
        httpResponser.host = urlConnection.getURL().getHost(); 
        httpResponser.path = urlConnection.getURL().getPath(); 
        httpResponser.port = urlConnection.getURL().getPort(); 
        httpResponser.protocol = urlConnection.getURL().getProtocol(); 
        httpResponser.query = urlConnection.getURL().getQuery(); 
        httpResponser.ref = urlConnection.getURL().getRef(); 
        httpResponser.userInfo = urlConnection.getURL().getUserInfo(); 
        httpResponser.content = new String(temp.toString().getBytes(), ecod); 
        httpResponser.contentEncoding = ecod; 
        httpResponser.code = urlConnection.getResponseCode(); 
        httpResponser.message = urlConnection.getResponseMessage(); 
        httpResponser.contentType = urlConnection.getContentType(); 
        httpResponser.method = urlConnection.getRequestMethod(); 
        httpResponser.connectTimeout = urlConnection.getConnectTimeout(); 
        httpResponser.readTimeout = urlConnection.getReadTimeout(); 
        httpResponser.headerFields = urlConnection.getHeaderFields();
    } catch (IOException e) { 
    	httpResponser.code = 404;
    } finally { 
        if (urlConnection != null) 
            urlConnection.disconnect(); 
    } 
    return httpResponser; 
}
 
源代码17 项目: jfinal-api-scaffold   文件: HttpRequester.java
/**
 * 处理响应
 *
 * @param urlConnection
 * @return 响应对象
 * @throws java.io.IOException
 */
private HttpResponse makeContent(String urlString,
                                 HttpURLConnection urlConnection) throws IOException {
    HttpResponse httpResponser = new HttpResponse();
    try {
        InputStream in = urlConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(in));
        httpResponser.contentCollection = new Vector<String>();
        StringBuffer temp = new StringBuffer();
        String line = bufferedReader.readLine();
        while (line != null) {
            httpResponser.contentCollection.add(line);
            temp.append(line).append("\r\n");
            line = bufferedReader.readLine();
        }
        bufferedReader.close();

        String ecod = urlConnection.getContentEncoding();
        if (ecod == null)
            ecod = this.defaultContentEncoding;

        httpResponser.urlString = urlString;

        httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
        httpResponser.file = urlConnection.getURL().getFile();
        httpResponser.host = urlConnection.getURL().getHost();
        httpResponser.path = urlConnection.getURL().getPath();
        httpResponser.port = urlConnection.getURL().getPort();
        httpResponser.protocol = urlConnection.getURL().getProtocol();
        httpResponser.query = urlConnection.getURL().getQuery();
        httpResponser.ref = urlConnection.getURL().getRef();
        httpResponser.userInfo = urlConnection.getURL().getUserInfo();

        httpResponser.content = new String(temp.toString().getBytes(), ecod);
        httpResponser.contentEncoding = ecod;
        httpResponser.code = urlConnection.getResponseCode();
        httpResponser.message = urlConnection.getResponseMessage();
        httpResponser.contentType = urlConnection.getContentType();
        httpResponser.method = urlConnection.getRequestMethod();
        httpResponser.connectTimeout = urlConnection.getConnectTimeout();
        httpResponser.readTimeout = urlConnection.getReadTimeout();

        return httpResponser;
    } catch (IOException e) {
        throw e;
    } finally {
        if (urlConnection != null)
            urlConnection.disconnect();
    }
}
 
源代码18 项目: constellation   文件: HttpsUtilities.java
/**
 * Get a {@code InputStream} from a {@code HttpsURLConnection} using the
 * appropriate input stream depending on whether the content encoding is
 * GZIP or not
 *
 * @param connection A HttpsURLConnection connection
 * @return An InputStream which could be a {@link GZIPInputStream} if the
 * content encoding is GZIP, {@link InflaterInputStream} of the encoding is
 * deflate, InputStream otherwise
 *
 * @throws IOException if an error occurs during the connection.
 */
public static InputStream getInputStream(final HttpURLConnection connection) throws IOException {
    final String encoding = connection.getContentEncoding();

    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        return new GZIPInputStream(connection.getInputStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        return new InflaterInputStream(connection.getInputStream(), new Inflater(Boolean.TRUE));
    } else {
        return connection.getInputStream();
    }
}
 
源代码19 项目: constellation   文件: HttpsUtilities.java
/**
 * Get a {@code InputStream} from a {@code HttpsURLConnection} using the
 * appropriate input stream depending on whether the content encoding is
 * GZIP or not
 *
 * @param connection A HttpsURLConnection connection
 * @return An InputStream which could be a {@link GZIPInputStream} if the
 * content encoding is GZIP, {@link InflaterInputStream} of the encoding is
 * deflate,InputStream otherwise which could also be null.
 *
 * @throws IOException if an error occurs during the connection.
 */
public static InputStream getErrorStream(final HttpURLConnection connection) throws IOException {
    final String encoding = connection.getContentEncoding();

    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        return new GZIPInputStream(connection.getErrorStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        return new InflaterInputStream(connection.getErrorStream(), new Inflater(Boolean.TRUE));
    } else {
        return connection.getErrorStream();
    }
}
 
源代码20 项目: jetty-runtime   文件: HttpUrlUtil.java
/**
 * Obtain the text (non-binary) response body from an {@link HttpURLConnection},
 * using the response provided charset.
 * <p>
 * Note: Normal HttpURLConnection doesn't use the provided charset properly.
 * </p>
 *
 * @param http the {@link HttpURLConnection} to obtain the response body from
 * @return the text of the response body
 * @throws IOException if unable to get the text of the response body
 */
public static String getResponseBody(HttpURLConnection http) throws IOException {
  Charset responseEncoding = StandardCharsets.UTF_8;
  if (http.getContentEncoding() != null) {
    responseEncoding = Charset.forName(http.getContentEncoding());
  }

  return IOUtils.toString(http.getInputStream(), responseEncoding);
}