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

The following examples show how to use java.net.HttpURLConnection#getContentEncoding() . 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
Source File: BiliRoamingApi.java    From BiliRoaming with GNU General Public License v3.0 6 votes vote down vote up
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
Source File: ScepClient.java    From xipki with 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 3
Source File: InternetRadioService.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 4
Source File: HttpUtils.java    From azeroth with 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 5
Source File: HttpUtils.java    From azeroth with 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 6
Source File: YandexTranslate.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * 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 7
Source File: Web.java    From appinventor-extensions with 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 8
Source File: NanoSparqlClient.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
		 * 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 9
Source File: HttpUtils.java    From jeesuite-libs with 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 10
Source File: SuggestionProvider.java    From SimplicityBrowser with 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 11
Source File: VmwareContext.java    From cloudstack with 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 12
Source File: HttpRequestSender.java    From JYTB with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: HttpsUtil.java    From xnx3 with 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 14
Source File: ResourceReportClient.java    From twill with 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 15
Source File: HttpUtil.java    From xnx3 with 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 16
Source File: HttpRequester.java    From jfinal-api-scaffold with 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 17
Source File: HttpRequester.java    From metrics with 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 18
Source File: HttpUrlUtil.java    From jetty-runtime with 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);
}
 
Example 19
Source File: HttpsUtilities.java    From constellation with 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 20
Source File: HttpsUtilities.java    From constellation with 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();
    }
}