Example usage for java.net HttpURLConnection getErrorStream

List of usage examples for java.net HttpURLConnection getErrorStream

Introduction

In this page you can find the example usage for java.net HttpURLConnection getErrorStream.

Prototype

public InputStream getErrorStream() 

Source Link

Document

Returns the error stream if the connection failed but the server sent useful data nonetheless.

Usage

From source file:com.exzogeni.dk.http.HttpTask.java

@NonNull
private static InputStream getInputStream(HttpURLConnection cn) {
    try {//www. j  ava 2 s. c  o m
        return cn.getInputStream();
    } catch (IOException e) {
        final InputStream stream = cn.getErrorStream();
        if (stream == null) {
            return new ByteArrayInputStream(new byte[0]);
        }
        return stream;
    }
}

From source file:org.almrangers.auth.aad.HttpClientHelper.java

public static String getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

    BufferedReader reader;//from  w w w  .  ja v a 2  s. c o m
    if (isSuccess) {
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } else {
        reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    }
    StringBuffer stringBuffer = new StringBuffer();
    String line = "";
    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line);
    }

    return stringBuffer.toString();
}

From source file:com.company.project.core.connector.HttpClientHelper.java

public static String getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

    BufferedReader reader = null;
    if (isSuccess) {
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } else {/*from   w w w .  j  a va2  s . c o m*/
        reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    }
    StringBuffer stringBuffer = new StringBuffer();
    String line = "";
    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line);
    }

    return stringBuffer.toString();
}

From source file:Main.java

protected static String getResponseAsString(HttpURLConnection conn, boolean responseError) throws IOException {
    String charset = getResponseCharset(conn.getContentType());
    String header = conn.getHeaderField("Content-Encoding");
    boolean isGzip = false;
    if (header != null && header.toLowerCase().contains("gzip")) {
        isGzip = true;/*  w  ww .j  a  v  a2s. c om*/
    }
    InputStream es = conn.getErrorStream();
    if (es == null) {
        InputStream input = conn.getInputStream();
        if (isGzip) {
            input = new GZIPInputStream(input);
        }
        return getStreamAsString(input, charset);
    } else {
        if (isGzip) {
            es = new GZIPInputStream(es);
        }
        String msg = getStreamAsString(es, charset);
        if (TextUtils.isEmpty(msg)) {
            throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else if (responseError) {
            return msg;
        } else {
            throw new IOException(msg);
        }
    }
}

From source file:org.droidparts.http.worker.HttpURLConnectionWorker.java

private static int connectAndGetResponseCodeOrThrow(HttpURLConnection conn) throws HTTPException {
    try {// ww  w .j a v  a2 s  .  c  o  m
        conn.connect();
        int respCode = conn.getResponseCode();
        if (isErrorResponseCode(respCode)) {
            HTTPInputStream is = HTTPInputStream.getInstance(conn, (conn.getErrorStream() != null));
            throw new HTTPException(respCode, is.readAndClose());
        }
        return respCode;
    } catch (HTTPException e) {
        throw e;
    } catch (Exception e) {
        throw new HTTPException(e);
    }

}

From source file:cn.garymb.wechatmoments.common.OkHttpStack.java

@SuppressWarnings("deprecation")
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;// w w w .  j a  v a2s  .c  o m
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:org.apache.flink.runtime.webmonitor.history.HistoryServerTest.java

public static String getFromHTTP(String url) throws Exception {
    URL u = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setConnectTimeout(100000);
    connection.connect();//from w w w. ja  v a  2 s  .  com
    InputStream is;
    if (connection.getResponseCode() >= 400) {
        // error!
        is = connection.getErrorStream();
    } else {
        is = connection.getInputStream();
    }

    return IOUtils.toString(is,
            connection.getContentEncoding() != null ? connection.getContentEncoding() : "UTF-8");
}

From source file:com.kubeiwu.commontool.khttp.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * //ww w . ja v a 2s . c o  m
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {

    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.android.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection/*from   www.j  a va 2s  .  c  o m*/
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection// w w  w.ja v a2s.  c o  m
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}