Java Code Examples for java.net.HttpURLConnection#getContentEncoding()

The following examples show how to use java.net.HttpURLConnection#getContentEncoding() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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;
}
 
Example 2
/**
 * 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;
}
 
Example 3
Source Project: azeroth   File: HttpUtils.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source Project: azeroth   File: HttpUtils.java    License: Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 5
/**
 * 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();
  }
}
 
Example 6
Source Project: appinventor-extensions   File: Web.java    License: Apache License 2.0 6 votes vote down vote up
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();
  }
}
 
Example 7
Source Project: jeesuite-libs   File: HttpUtils.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source Project: SimplicityBrowser   File: SuggestionProvider.java    License: MIT License 6 votes vote down vote up
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;
}
 
Example 9
/**
		 * 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();
			}

		}
 
Example 10
Source Project: xipki   File: ScepClient.java    License: Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 11
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);
}
 
Example 12
Source Project: xnx3   File: HttpsUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 得到响应对象
 * @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(); 
    } 
}
 
Example 13
Source Project: twill   File: ResourceReportClient.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source Project: cloudstack   File: VmwareContext.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source Project: metrics   File: HttpRequester.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 得到响应对象
 *
 * @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();
    }
}
 
Example 16
Source Project: xnx3   File: HttpUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 得到响应对象
 * @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; 
}
 
Example 17
Source Project: jfinal-api-scaffold   File: HttpRequester.java    License: MIT License 4 votes vote down vote up
/**
 * 处理响应
 *
 * @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();
    }
}
 
Example 18
Source Project: constellation   File: HttpsUtilities.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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();
    }
}
 
Example 19
Source Project: constellation   File: HttpsUtilities.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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();
    }
}
 
Example 20
Source Project: jetty-runtime   File: HttpUrlUtil.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}