List of usage examples for java.net HttpURLConnection getErrorStream
public InputStream getErrorStream()
From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java
/** * Performs a POST request to the specified URL and returns the result. * <p />/*from ww w . j ava2s .c om*/ * The POST data will be encoded in UTF-8 as the specified contentType. The response will be parsed as UTF-8. * If the server returns an error but still provides a body, the body will be returned as normal. * If the server returns an error without any body, a relevant {@link java.io.IOException} will be thrown. * * @param url URL to submit the POST request to * @param post POST data in the correct format to be submitted * @param contentType Content type of the POST data * @return Raw text response from the server * @throws IOException The request was not successful */ public static String performPostRequest(final URL url, final String post, final String contentType) throws IOException { Validate.notNull(url); Validate.notNull(post); Validate.notNull(contentType); final HttpURLConnection connection = createUrlConnection(url); final byte[] postAsBytes = post.getBytes(Charsets.UTF_8); connection.setRequestProperty("Content-Type", contentType + "; charset=utf-8"); connection.setRequestProperty("Content-Length", "" + postAsBytes.length); connection.setDoOutput(true); OutputStream outputStream = null; try { outputStream = connection.getOutputStream(); IOUtils.write(postAsBytes, outputStream); } finally { IOUtils.closeQuietly(outputStream); } InputStream inputStream = null; try { inputStream = connection.getInputStream(); final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } catch (final IOException e) { IOUtils.closeQuietly(inputStream); inputStream = connection.getErrorStream(); if (inputStream != null) { final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } else { throw e; } } finally { IOUtils.closeQuietly(inputStream); } }
From source file:org.kymjs.kjframe.http.httpclient.HttpRequestBuilder.java
/** * Open the error {@link InputStream} of an Http response. This method * supports GZIP and DEFLATE responses./*from w w w .j a v a 2 s. co m*/ */ private static InputStream getErrorStream(HttpURLConnection conn) throws IOException { final List<String> contentEncodingValues = conn.getHeaderFields().get("Content-Encoding"); if (contentEncodingValues != null) { for (final String contentEncoding : contentEncodingValues) { if (contentEncoding != null) { if (contentEncoding.contains("gzip")) { return new GZIPInputStream(conn.getErrorStream()); } if (contentEncoding.contains("deflate")) { return new InflaterInputStream(conn.getErrorStream(), new Inflater(true)); } } } } return conn.getErrorStream(); }
From source file:com.iStudy.Study.Renren.Util.java
/** * ??http/*from ww w . j ava 2s .c o m*/ * * @param url * @param method GET POST * @param params * @return */ public static String openUrl(String url, String method, Bundle params) { if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } String response = ""; try { Log.d(LOG_TAG, method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", USER_AGENT_SDK); if (!method.equals("GET")) { conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8")); } InputStream is = null; int responseCode = conn.getResponseCode(); if (responseCode == 200) { is = conn.getInputStream(); } else { is = conn.getErrorStream(); } response = read(is); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } return response; }
From source file:com.cloudbees.mtslaves.client.RemoteReference.java
/** * Wrap the exception with one which includes the error message from the server. * @param con the connection throwing the error * @param e the original exception/* ww w .j av a2 s . c o m*/ * @return the wrapped exception * @throws IOException is unexpected */ protected IOException wrappedException(HttpURLConnection con, IOException e) throws IOException { InputStream es = con.getErrorStream(); String payload = es != null ? IOUtils.toString(es) : "(no data)"; return new IOException(e.getMessage() + " Error response: " + payload, e); }
From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java
/** * Performs a POST request to the specified URL and returns the result. * <p />/*from ww w. j a v a2s . c o m*/ * The POST data will be encoded in UTF-8 as the specified contentType. The response will be parsed as UTF-8. * If the server returns an error but still provides a body, the body will be returned as normal. * If the server returns an error without any body, a relevant {@link java.io.IOException} will be thrown. * * @param url URL to submit the POST request to * @param post POST data in the correct format to be submitted * @param contentType Content type of the POST data * @return Raw text response from the server * @throws IOException The request was not successful */ public static String performPostRequestWithAuth(final URL url, final String post, final String contentType, final String auth) throws IOException { Validate.notNull(url); Validate.notNull(post); Validate.notNull(contentType); final HttpURLConnection connection = createUrlConnection(url); final byte[] postAsBytes = post.getBytes(Charsets.UTF_8); connection.setRequestProperty("Authorization", auth); connection.setRequestProperty("Content-Type", contentType + "; charset=utf-8"); connection.setRequestProperty("Content-Length", "" + postAsBytes.length); connection.setDoOutput(true); OutputStream outputStream = null; try { outputStream = connection.getOutputStream(); IOUtils.write(postAsBytes, outputStream); } finally { IOUtils.closeQuietly(outputStream); } InputStream inputStream = null; try { inputStream = connection.getInputStream(); final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } catch (final IOException e) { IOUtils.closeQuietly(inputStream); inputStream = connection.getErrorStream(); if (inputStream != null) { final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } else { throw e; } } finally { IOUtils.closeQuietly(inputStream); } }
From source file:com.cloudbees.mtslaves.client.RemoteReference.java
protected void drain(HttpURLConnection con) throws IOException { if (con.getResponseCode() >= 500) { String error = IOUtils.toString(con.getErrorStream()); con.getErrorStream().close();/*from www.j a va2 s . c om*/ throw new IOException("Error received:" + con.getResponseCode() + "\n" + error); } else { IOUtils.copy(con.getInputStream(), new NullOutputStream()); con.getInputStream().close(); } }
From source file:com.appdynamics.monitors.ehcache.EhcacheRESTWrapper.java
private void printError(HttpURLConnection connection, String cacheServerUrl) { if (connection != null) { try {/*from www . j a v a2 s . co m*/ InputStream errorStream = connection.getErrorStream(); BufferedReader br = new BufferedReader(new InputStreamReader(errorStream)); String temp; logger.error("Error while invoking the url " + cacheServerUrl); StringBuilder sb = new StringBuilder(); while ((temp = br.readLine()) != null) { sb.append(temp).append("\n"); } logger.error("The server Response is " + sb.toString()); } catch (Exception e) { logger.error("Error while printing response ", e); } } }
From source file:com.cisco.gerrit.plugins.slack.client.WebhookClient.java
private InputStream getResponseStream(HttpURLConnection connection) { try {/*from w w w . j a v a2s . c o m*/ return connection.getInputStream(); } catch (IOException e) { return connection.getErrorStream(); } }
From source file:com.gliffy.restunit.http.JavaHttp.java
private byte[] readBody(HttpURLConnection connection) throws IOException { InputStream is = connection.getInputStream(); is = connection.getInputStream();//from w ww . j av a 2s .co m if (is == null) is = connection.getErrorStream(); if (is == null) return null; ByteArrayOutputStream os = new ByteArrayOutputStream(); int ch = is.read(); while (ch != -1) { os.write(ch); ch = is.read(); } return os.toByteArray(); }
From source file:es.tid.cep.esperanza.Utils.java
public static boolean DoHTTPPost(String urlStr, String content) { try {/*w w w . ja va 2 s. c o m*/ URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug("action http response " + code + " " + message); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.debug("action response body: " + line); } input.close(); return true; } else { logger.error("action response is not OK: " + code + " " + message); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error("action error response body: " + line); } error.close(); return false; } } catch (MalformedURLException me) { logger.error("exception MalformedURLException: " + me.getMessage()); return false; } catch (IOException ioe) { logger.error("exception IOException: " + ioe.getMessage()); return false; } }