List of usage examples for java.net HttpURLConnection getContentEncoding
public String getContentEncoding()
From source file:org.mule.modules.cdcsoftware.HTTPHelper.java
public static String doHTTP(String method, String urlString, int timeout, String body) throws IOException { String result = null;// w ww . j av a 2s .co m URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(20000); conn.setReadTimeout(timeout * 1000); conn.setUseCaches(false); conn.setDoInput(true); conn.setRequestProperty("Content-Type", TEXT_XML_UTF_8); conn.setRequestProperty("Accept-Encoding", GZIP_DEFLATE); String userpass = "username" + ":" + "password"; String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes())); conn.setRequestProperty("Authorization", basicAuth); conn.setRequestMethod(method); if ("POST".equals(method)) { conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(body.getBytes(UTF_8).length)); OutputStream post = conn.getOutputStream(); post.write(body.getBytes("UTF-8")); post.flush(); post.close(); } else { conn.setDoOutput(false); } int responseCode = conn.getResponseCode(); if (responseCode == 200) { String encoding = conn.getContentEncoding(); InputStream resultingInputStream; if (encoding != null && encoding.equalsIgnoreCase(GZIP)) { resultingInputStream = new GZIPInputStream(conn.getInputStream()); } else { resultingInputStream = conn.getInputStream(); } result = getResponseAsString(resultingInputStream); } return result; }
From source file:io.druid.server.initialization.JettyTest.java
@Test public void testGzipCompression() throws Exception { final URL url = new URL("http://localhost:" + port + "/default"); final HttpURLConnection get = (HttpURLConnection) url.openConnection(); get.setRequestProperty("Accept-Encoding", "gzip"); Assert.assertEquals("gzip", get.getContentEncoding()); final HttpURLConnection post = (HttpURLConnection) url.openConnection(); post.setRequestProperty("Accept-Encoding", "gzip"); post.setRequestMethod("POST"); Assert.assertEquals("gzip", post.getContentEncoding()); final HttpURLConnection getNoGzip = (HttpURLConnection) url.openConnection(); Assert.assertNotEquals("gzip", getNoGzip.getContentEncoding()); final HttpURLConnection postNoGzip = (HttpURLConnection) url.openConnection(); postNoGzip.setRequestMethod("POST"); Assert.assertNotEquals("gzip", postNoGzip.getContentEncoding()); }
From source file:common.net.volley.toolbox.HurlStack.java
/** * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}. * @param connection// w w w . j a v a 2 s. com * @return an HttpEntity populated with data from <code>connection</code>. */ private static HttpEntity entityFromConnection(HttpURLConnection connection) throws IOException { BasicHttpEntity entity = new BasicHttpEntity(); ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream rawStream = null; try { rawStream = connection.getInputStream(); rawStream = stethoManager.interpretResponseStream(rawStream); InputStream decompressedStream = applyDecompressionIfApplicable(connection, rawStream); if (decompressedStream != null) { copy(decompressedStream, out, new byte[1024]); } entity.setContent(new ByteArrayInputStream(out.toByteArray())); } catch (IOException ioe) { rawStream = connection.getErrorStream(); entity.setContent(rawStream); } finally { // if(rawStream != null) { // rawStream.close(); // } } entity.setContentLength(connection.getContentLength()); entity.setContentEncoding(connection.getContentEncoding()); entity.setContentType(connection.getContentType()); return entity; }
From source file:io.jari.geenstijl.API.API.java
public static String postUrl(String url, List<NameValuePair> params, String cheader, boolean refererandorigin) throws IOException { HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection(); http.setRequestMethod("POST"); http.setDoInput(true);/*from www. j a va 2s. c o m*/ http.setDoOutput(true); if (cheader != null) http.setRequestProperty("Cookie", cheader); if (refererandorigin) { http.setRequestProperty("Referer", "http://www.geenstijl.nl/mt/archieven/2014/01/brein_chanteert_ondertitelaars.html"); http.setRequestProperty("Origin", "http://www.geenstijl.nl"); } OutputStream os = http.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(getQuery(params)); writer.flush(); writer.close(); os.close(); http.connect(); InputStream in = http.getInputStream(); String encoding = http.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; int len = 0; while ((len = in.read(buf)) != -1) { baos.write(buf, 0, len); } return new String(baos.toByteArray(), encoding); }
From source file:net.nikr.eve.jeveasset.io.online.CitadelGetter.java
private void updateCache(UpdateTask updateTask) { LOG.info("Citadels updating:"); if (citadelSettings.getNextUpdate().after(new Date()) && !Settings.get().isForceUpdate() && !Program.isForceUpdate()) { //Check if we can update now if (updateTask != null) { updateTask.addError(DialoguesUpdate.get().citadel(), "Not allowed yet.\r\n(Fix: Just wait a bit)"); }//from ww w . j a v a2 s .co m LOG.info(" Citadels failed to update (NOT ALLOWED YET)"); return; } //Update citadel InputStream in = null; try { //Update from API ObjectMapper mapper = new ObjectMapper(); //create once, reuse URL url = new URL(URL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Accept-Encoding", "gzip"); long contentLength = con.getContentLengthLong(); String contentEncoding = con.getContentEncoding(); InputStream inputStream = new UpdateTaskInputStream(con.getInputStream(), contentLength, updateTask); if ("gzip".equals(contentEncoding)) { in = new GZIPInputStream(inputStream); } else { in = inputStream; } Map<Long, Citadel> results = mapper.readValue(in, new TypeReference<Map<Long, Citadel>>() { }); if (results != null) { //Updated OK for (Map.Entry<Long, Citadel> entry : results.entrySet()) { citadelSettings.put(entry.getKey(), entry.getValue()); } } citadelSettings.setNextUpdate(); saveXml(); LOG.info(" Updated citadels for jEveAssets"); } catch (IOException ex) { if (updateTask != null) { updateTask.addError(DialoguesUpdate.get().citadel(), ex.getMessage()); } LOG.error(" Citadels failed to update", ex); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { //No problem... } } } }
From source file:com.adamrosenfield.wordswithcrosses.net.DerStandardDownloader.java
protected Reader getURLReader(HttpURLConnection conn) throws IOException, MalformedURLException, UnsupportedEncodingException { InputStream in = (InputStream) conn.getContent(); String encoding = conn.getContentEncoding(); String type = conn.getContentType(); if (encoding == null) { Matcher m = P_CHARSET_IN_TYPE.matcher(type); if (m.matches()) { encoding = m.group(1);/* w w w . ja v a 2s. com*/ } else { encoding = "ISO-8859-1"; } } Reader r = new InputStreamReader(in, encoding); return r; }
From source file:org.apache.druid.server.initialization.JettyTest.java
@Test public void testGzipResponseCompression() throws Exception { final URL url = new URL("http://localhost:" + port + "/default"); final HttpURLConnection get = (HttpURLConnection) url.openConnection(); get.setRequestProperty("Accept-Encoding", "gzip"); Assert.assertEquals("gzip", get.getContentEncoding()); Assert.assertEquals(DEFAULT_RESPONSE_CONTENT, IOUtils.toString(new GZIPInputStream(get.getInputStream()), StandardCharsets.UTF_8)); final HttpURLConnection post = (HttpURLConnection) url.openConnection(); post.setRequestProperty("Accept-Encoding", "gzip"); post.setRequestMethod("POST"); Assert.assertEquals("gzip", post.getContentEncoding()); Assert.assertEquals(DEFAULT_RESPONSE_CONTENT, IOUtils.toString(new GZIPInputStream(post.getInputStream()), StandardCharsets.UTF_8)); final HttpURLConnection getNoGzip = (HttpURLConnection) url.openConnection(); Assert.assertNotEquals("gzip", getNoGzip.getContentEncoding()); Assert.assertEquals(DEFAULT_RESPONSE_CONTENT, IOUtils.toString(getNoGzip.getInputStream(), StandardCharsets.UTF_8)); final HttpURLConnection postNoGzip = (HttpURLConnection) url.openConnection(); postNoGzip.setRequestMethod("POST"); Assert.assertNotEquals("gzip", postNoGzip.getContentEncoding()); Assert.assertEquals(DEFAULT_RESPONSE_CONTENT, IOUtils.toString(postNoGzip.getInputStream(), StandardCharsets.UTF_8)); }
From source file:com.adaptris.core.http.client.net.StandardHttpProducer.java
private String getContentEncoding(HttpURLConnection http) { if (http.getContentEncoding() != null) { return http.getContentEncoding(); }/*from w ww .ja v a 2 s .com*/ // Parse Content-Type header for encoding try { ContentType contentType = new ContentType(http.getContentType()); if (!isEmpty(contentType.getParameter(PARAM_CHARSET))) { return contentType.getParameter(PARAM_CHARSET); } } catch (ParseException e) { log.trace("Unable to parse Content-Type header \"{}\": {}", http.getContentType(), e.toString()); } return null; }
From source file:org.treasurehunter.activity.cachelist.MenuActionMyLocationUpdate.java
public String SendRequest() throws Exception { final URL url = getURL(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(6000);/*from ww w.j a va2s .c o m*/ conn.setConnectTimeout(6000); int responseCode = conn.getResponseCode(); // Will wait for response InputStream in = conn.getInputStream(); String contentEncoding = conn.getContentEncoding(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } Log.d("Location Update ", sb.toString()); return (sb.toString()); }
From source file:org.jumpmind.metl.core.runtime.resource.HttpDirectory.java
@Override public InputStream getInputStream(String relativePath, boolean mustExist) { try {/*from w ww . j a v a 2 s . c o m*/ HttpURLConnection httpConnection = buildHttpUrlConnection(relativePath); int responseCode = httpConnection.getResponseCode(); if (responseCode == 200) { String type = httpConnection.getContentEncoding(); InputStream in = httpConnection.getInputStream(); if (!isBlank(type) && type.equals("gzip")) { in = new GZIPInputStream(in); } return in; } else { throw new IoException("Received an unexpected response code of " + responseCode); } } catch (IOException e) { throw new IoException(e); } }