Java HTTP Read getInputStream(HttpURLConnection connection)

Here you can find the source of getInputStream(HttpURLConnection connection)

Description

Gets the correct java.io.InputStream based on urlConnection encoding.

License

Apache License

Parameter

Parameter Description
connection connection

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.java.io.InputStream

Return

inputstream from encoding. If not supported returns normal

Declaration

public static InputStream getInputStream(HttpURLConnection connection) throws IOException 

Method Source Code


//package com.java2s;
/* ===================================================
 * Copyright 2013 Kroboth Software/*w  w w.ja va  2  s. c  o  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== 
 */

import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;

import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public class Main {
    /**
     * Gets the correct {@link java.io.InputStream} based on
     * <code>urlConnection</code> encoding.
     * 
     * @param connection
     *            connection
     * @return inputstream from encoding. If not supported returns normal
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     *             {@link java.io.InputStream}
     * @since SNC 1.0
     */
    public static InputStream getInputStream(HttpURLConnection connection) throws IOException {
        if (connection.getRequestMethod().equals("HEAD"))
            return null;
        String encoding = connection.getContentEncoding();
        if (encoding == null)
            return connection.getInputStream();
        else if (encoding.equalsIgnoreCase("gzip"))
            return new GZIPInputStream(connection.getInputStream());
        else if (encoding.equalsIgnoreCase("deflate"))
            return new InflaterInputStream(connection.getInputStream(), new Inflater(true));

        return null;

    }
}

Related

  1. getContentWithPost(String urls, Map pv)
  2. getData(String urlString)
  3. getHtmlByteArray(final String url)
  4. getHtmlContent(String url)
  5. getInputStream(HttpURLConnection conn)
  6. getInputStream(String myUrl)
  7. getInputStream(String url)
  8. getInputStreamFromURL(String urlname)
  9. getInputStreamFromUrl(String urlString)