List of usage examples for java.net HttpURLConnection getErrorStream
public InputStream getErrorStream()
From source file:com.parasoft.em.client.impl.JSONClient.java
protected JSONObject doDelete(String restPath) throws IOException { HttpURLConnection connection = getConnection(restPath); connection.setRequestMethod("DELETE"); int responseCode = connection.getResponseCode(); if (responseCode / 100 == 2) { return getResponseJSON(connection.getInputStream()); } else {/*from w ww.j av a2s.co m*/ String errorMessage = getResponseString(connection.getErrorStream()); throw new IOException(restPath + ' ' + responseCode + '\n' + errorMessage); } }
From source file:org.apache.ambari.server.view.ViewURLStreamProvider.java
private InputStream getInputStream(String spec, String requestMethod, Map<String, String> headers, byte[] info) throws IOException { if (!isProxyCallAllowed(spec)) { LOG.warn("Call to " + spec + " is not allowed. See ambari.properties proxy.allowed.hostports."); throw new IOException( "Call to " + spec + " is not allowed. See ambari.properties proxy.allowed.hostports."); }//w w w .j a va 2 s . c o m HttpURLConnection connection = getHttpURLConnection(spec, requestMethod, headers, info); int responseCode = connection.getResponseCode(); return responseCode >= ProxyService.HTTP_ERROR_RANGE_START ? connection.getErrorStream() : connection.getInputStream(); }
From source file:piuk.MyRemoteWallet.java
private static String fetchURL(String URL) throws Exception { if (URL.indexOf("?") > 0) { URL += "&api_code=" + getApiCode(); } else {//from w w w.java 2s . c om URL += "?api_code=" + getApiCode(); } URL url = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestMethod("GET"); connection.setConnectTimeout(180000); connection.setReadTimeout(180000); connection.setInstanceFollowRedirects(false); connection.connect(); if (connection.getResponseCode() == 200) return IOUtils.toString(connection.getInputStream(), "UTF-8"); else if (connection.getResponseCode() == 500) throw new Exception("Error From Server: " + IOUtils.toString(connection.getErrorStream(), "UTF-8")); else throw new Exception("Unknown response from server (" + connection.getResponseCode() + ") " + IOUtils.toString(connection.getErrorStream(), "UTF-8")); } finally { connection.disconnect(); } }
From source file:org.talend.components.dataprep.connection.DataPrepConnectionHandler.java
private String extractResponseInformationAndConsumeResponse(HttpURLConnection connection) throws IOException { InputStream is = null;// w w w . j a va2 s. com try { try { is = connection.getInputStream(); } catch (IOException e) { is = connection.getErrorStream(); } return extractResponseInformationAndConsumeResponse(is); } finally { if (is != null) { is.close(); } } }
From source file:org.mozilla.mozstumbler.service.core.http.HttpUtil.java
private byte[] getContentBody(HttpURLConnection httpURLConnection) throws IOException { InputStream in = null;//w ww . ja v a 2s. c om try { in = new BufferedInputStream(httpURLConnection.getInputStream()); } catch (Exception ex) { in = httpURLConnection.getErrorStream(); } if (in == null) { return new byte[] {}; } ByteArrayOutputStream dataStream = null; OutputStream out = null; try { dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, StreamUtils.IO_BUFFER_SIZE); StreamUtils.copy(in, out); out.flush(); } finally { if (out != null) { try { out.close(); } catch (IOException ioEx) { Log.e(LOG_TAG, "Error closing tile output stream.", ioEx); } } if (in != null) { try { in.close(); } catch (IOException ioEx) { Log.e(LOG_TAG, "Error closing tile output stream.", ioEx); } } } return dataStream.toByteArray(); }
From source file:eu.fusepool.p3.transformer.client.TransformerClientImpl.java
private Entity getResponseEntity(HttpURLConnection connection) throws IOException, MimeTypeParseException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (connection.getResponseCode() < 400) { IOUtils.copy(connection.getInputStream(), baos); } else {/*from w ww. j a v a2 s. c o m*/ IOUtils.copy(connection.getErrorStream(), baos); } final byte[] bytes = baos.toByteArray(); final String resultContentTypeString = connection.getHeaderField("Content-Type"); final MimeType resultType = resultContentTypeString != null ? new MimeType(resultContentTypeString) : new MimeType("application", "octet-stream"); return new InputStreamEntity() { @Override public MimeType getType() { return resultType; } @Override public InputStream getData() throws IOException { return new ByteArrayInputStream(bytes); } }; }
From source file:br.com.anteros.social.instagram.AnterosInstagramSession.java
private String checkInputStream(HttpURLConnection connection) { String errorType = null, code = null, errorMessage = null; InputStream inputStream = connection.getErrorStream(); String response = streamToString(inputStream); try {/* w w w. j a v a 2 s .c o m*/ JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue(); JSONObject jsonResponse = jsonObject.getJSONObject("meta"); if (jsonResponse.has(ERROR_TYPE)) { errorType = jsonResponse.getString(ERROR_TYPE); } if (jsonResponse.has(CODE)) { code = jsonResponse.getString(CODE); } if (jsonResponse.has(ERROR_MESSAGE)) { errorMessage = jsonResponse.getString(ERROR_MESSAGE); } return "ERROR TYPE: " + errorType + " ERROR CODE: " + code + " ERROR MESSAGE: " + errorMessage; } catch (JSONException e) { return e.getMessage(); } }
From source file:com.adanac.module.blog.api.HttpApiHelper.java
public static void baiduPush(int remain) { String pushUrl = DaoFactory.getDao(HtmlPageDao.class).findPushUrl(); if (pushUrl == null) { if (logger.isInfoEnabled()) { logger.info("all html page has been pushed!"); }/* w w w. j a va2 s .c om*/ return; } if (remain <= 0) { if (logger.isInfoEnabled()) { logger.info("there has no remain[" + remain + "]!"); } return; } if (logger.isInfoEnabled()) { logger.info("find push url : " + pushUrl); } String url = "http://data.zz.baidu.com/urls?site=" + site + "&token=" + token; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/plain"); OutputStream outputStream = connection.getOutputStream(); outputStream.write(pushUrl.getBytes("UTF-8")); outputStream.write("\r\n".getBytes("UTF-8")); outputStream.flush(); int status = connection.getResponseCode(); if (logger.isInfoEnabled()) { logger.info("baidu-push response code : " + status); } if (status == HttpServletResponse.SC_OK) { String response = IOUtil.read(connection.getInputStream()); if (logger.isInfoEnabled()) { logger.info("baidu-push response : " + response); } JSONObject result = JSONObject.fromObject(response); if (result.getInt("success") >= 1) { DaoFactory.getDao(HtmlPageDao.class).updateIsPush(pushUrl); } else { logger.warn("push url failed : " + pushUrl); } baiduPush(result.getInt("remain")); } else { logger.error("baidu-push error : " + IOUtil.read(connection.getErrorStream())); } } catch (Exception e) { logger.error("baidu push failed ...", e); } }
From source file:com.joyent.manta.client.MantaClientSigningIT.java
@Test public final void testCanCreateSignedGETUriFromPath() throws IOException { if (config.isClientEncryptionEnabled()) { throw new SkipException("Signed URLs are not decrypted by the client"); }//from w w w. j a va2 s . c o m final String name = UUID.randomUUID().toString(); final String path = testPathPrefix + name; mantaClient.put(path, TEST_DATA); // This will throw an error if the newly inserted object isn't present mantaClient.head(path); Instant expires = Instant.now().plus(1, ChronoUnit.HOURS); URI uri = mantaClient.getAsSignedURI(path, "GET", expires); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); try (InputStream is = connection.getInputStream()) { connection.setReadTimeout(3000); connection.connect(); if (connection.getResponseCode() != 200) { Assert.fail(IOUtils.toString(connection.getErrorStream(), Charset.defaultCharset())); } String actual = IOUtils.toString(is, Charset.defaultCharset()); Assert.assertEquals(actual, TEST_DATA); } finally { connection.disconnect(); } }
From source file:hmock.BasicHMockTest.java
@Test public void testCanSetResponseCode() throws Exception { HMock.respond().status(400).body("bad request").when().get("/statuscode"); URL request = new URL("http://localhost:7357/statuscode"); HttpURLConnection conn = (HttpURLConnection) request.openConnection(); assertEquals(400, conn.getResponseCode()); /* /*w ww . j a v a 2 s.c om*/ * need to read error after invoke a method that will initiate the * http connection (.getResponseCode). Oh HttpURLConnection API... * */ String responseBody = IOUtils.toString(conn.getErrorStream()); assertEquals("bad request", responseBody); }