List of usage examples for java.net URLConnection getContentEncoding
public String getContentEncoding()
From source file:de.thecamper.android.androidtools.UpdateChecker.java
protected Boolean doInBackground(Void... params) { try {//w ww .java 2s. co m // get the version code from a Get-Request URL url = new URL(versionURL); URLConnection con = url.openConnection(); InputStream is = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; int versionNumber = Integer.parseInt(IOUtils.toString(is, encoding)); // if it is a newer version out there return true if (versionNumber > context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode) { if (buildNumberIncluded) appURL = appURL.replace("#", String.valueOf(versionNumber)); return true; } else return false; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } catch (NameNotFoundException e) { e.printStackTrace(); } return false; }
From source file:me.mast3rplan.phantombot.twitch.TwitchAPI.java
public JSONObject postObject(String url) throws IOException { URLConnection connection = new URL(url).openConnection(); connection.setUseCaches(false);//from ww w. ja v a 2 s . c o m connection.setDefaultUseCaches(false); connection.addRequestProperty(url, url); String content = IOUtils.toString(connection.getInputStream(), connection.getContentEncoding()); return new JSONObject(content); }
From source file:org.norvelle.addressdiscoverer.gui.threading.ExtractIndividualsFromUrlWorker.java
/** * Run the classification process on the contents of a remote web page. * //from w ww . j av a2 s . c o m * @param parent * @param uri * @param department * @throws java.net.MalformedURLException * @throws java.net.URISyntaxException */ public ExtractIndividualsFromUrlWorker(EmailDiscoveryPanel parent, String uri, Department department, boolean useSequentialParser) throws MalformedURLException, URISyntaxException, IOException { super(parent, null, department, useSequentialParser); StructuredPageContactLinkLocator.baseUrl = uri; parent.getjStageNameLabel().setText("Reading remote web page"); URL u = new URL(uri); // this would check for the protocol u.toURI(); URLConnection con = u.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; " + "en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); File tempDir = FileUtils.getTempDirectory(); File tempFile = new File(tempDir.getAbsolutePath() + File.separator + "classifier.html.tmp"); FileUtils.write(tempFile, body, encoding); this.fileToClassify = tempFile; parent.getjBytesReceivedLabel().setText(String.valueOf(body.length())); }
From source file:gate.corpora.DocumentContentImpl.java
/** Contruction from URL and offsets. */ public DocumentContentImpl(URL u, String encoding, Long start, Long end) throws IOException { int readLength = 0; char[] readBuffer = new char[INTERNAL_BUFFER_SIZE]; BufferedReader uReader = null; InputStream uStream = null;/* w w w. j a v a 2 s. c o m*/ StringBuffer buf = new StringBuffer(); long s = 0, e = Long.MAX_VALUE; if (start != null && end != null) { s = start.longValue(); e = end.longValue(); } try { URLConnection conn = u.openConnection(); uStream = conn.getInputStream(); if ("gzip".equals(conn.getContentEncoding())) { uStream = new GZIPInputStream(uStream); } if (encoding != null && !encoding.equalsIgnoreCase("")) { uReader = new BomStrippingInputStreamReader(uStream, encoding, INTERNAL_BUFFER_SIZE); } else { uReader = new BomStrippingInputStreamReader(uStream, INTERNAL_BUFFER_SIZE); } ; // 1. skip S characters uReader.skip(s); // 2. how many character shall I read? long toRead = e - s; // 3. read gtom source into buffer while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) { if (toRead < readLength) { //well, if toRead(long) is less than readLenght(int) //then there can be no overflow, so the cast is safe readLength = (int) toRead; } buf.append(readBuffer, 0, readLength); toRead -= readLength; } } finally { // 4.close reader IOUtils.closeQuietly(uReader); IOUtils.closeQuietly(uStream); } content = new String(buf); originalContent = content; }
From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java
protected Charset getCharset(final URLConnection urlConnection) { Charset charset = null;//from w w w .j a v a 2 s .co m // Prio 1: content encoding specified in header String encoding = urlConnection.getContentEncoding(); // Prio 2: charset specified within content type (specified in header) if (encoding == null) { final String contentType = urlConnection.getHeaderField("Content-Type"); if (contentType != null) { for (final String metaTagParameter : contentType.replace(" ", "").split(";")) { if (metaTagParameter.startsWith("charset=")) { encoding = metaTagParameter.split("=", 2)[1]; break; } } } } // Prio 3: default charset if (encoding == null) { return Charset.defaultCharset(); } // Create charset try { charset = Charset.forName(encoding); } catch (final Exception ex) { LOG.warn("Unsupported encoding " + encoding + " by " + urlConnection.getURL().toExternalForm(), ex); } // Fallback if (charset == null) { charset = Charset.defaultCharset(); } // Done return charset; }
From source file:org.alfresco.test.util.AlfrescoHttpClient.java
/** * Generates an Alfresco Authentication Ticket based on the user name and password passed in. * /*from w w w . j a va2 s.c om*/ * @param username user identifier * @param password user password * @throws ParseException if error * @throws IOException if error * @return Sting authentication ticket */ public String getAlfTicket(String username, String password) throws IOException, ParseException { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { throw new IllegalArgumentException("Username and password are required"); } String targetUrl = apiUrl + "login?u=" + username + "&pw=" + password + "&format=json"; try { URL url = new URL(apiUrl + "login?u=" + username + "&pw=" + password + "&format=json"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? UTF_8_ENCODING : encoding; String json = IOUtils.toString(in, encoding); JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(json); JSONObject data = (JSONObject) obj.get("data"); return (String) data.get("ticket"); } catch (IOException e) { logger.error(String.format("Unable to generate ticket, url: %s", targetUrl), e); } throw new RuntimeException("Unable to get ticket"); }
From source file:net.solarnetwork.node.support.HttpClientSupport.java
/** * Get an InputStream from a URLConnection response, handling compression. * /*from ww w.j a v a 2 s . c om*/ * <p> * This method handles decompressing the response if the encoding is set to * {@code gzip} or {@code deflate}. * </p> * * @param conn * the URLConnection * @return the InputStream * @throws IOException * if any IO error occurs */ protected InputStream getInputStreamFromURLConnection(URLConnection conn) throws IOException { String enc = conn.getContentEncoding(); String type = conn.getContentType(); if (conn instanceof HttpURLConnection) { HttpURLConnection httpConn = (HttpURLConnection) conn; if (httpConn.getResponseCode() < 200 || httpConn.getResponseCode() > 299) { log.info("Non-200 HTTP response from {}: {}", conn.getURL(), httpConn.getResponseCode()); } } log.trace("Got content type [{}] encoded as [{}]", type, enc); InputStream is = conn.getInputStream(); if ("gzip".equalsIgnoreCase(enc)) { is = new GZIPInputStream(is); } else if ("deflate".equalsIgnoreCase("enc")) { is = new DeflaterInputStream(is); } return is; }
From source file:com.knowarth.portlet.downloadinterceptor.DownloadInterceptorPortlet.java
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException { //Setting out email parameters String emailAdd = resourceRequest.getParameter("emailAddress"); String visitorName = resourceRequest.getParameter("visitorName"); String cmpName = resourceRequest.getParameter("companyName"); String phoneNumber = resourceRequest.getParameter("phoneNumber"); String comments = resourceRequest.getParameter("comments"); //Getting the preferences and reading URL Location reading from edit mode PortletPreferences prefs = resourceRequest.getPreferences(); String resourceurl = prefs.getValue("resourceurl", ""); String caseStudyName = prefs.getValue("caseStudyName", ""); String receiveEmailAdd = prefs.getValue("receiverEmailAdd", ""); //emailBodyContent Return the template stored in preferences edit mode. String emailBodyContent = prefs.getValue("emailBodyTemplate", ""); String emailsubj = prefs.getValue("emailSubjectTemplate", ""); String resExtension = prefs.getValue("ResourceExtension", ""); String body = StringUtil.replace(emailBodyContent, new String[] { "[$VISITOR_NAME$]", "[$COMPANY_NAME$]", "[$EMAIL_ADDRESS$]", "[$PHONE_NUMBER$]", "[$COMMENTS$]", "[$CASE_STUDY_NAME$]" }, new String[] { visitorName, cmpName, emailAdd, phoneNumber, comments, caseStudyName }); //Setting out body and email parameters String subject = StringUtil.replace(emailsubj, new String[] { "[$VISITOR_NAME$]", "[$CASE_STUDY_NAME$]" }, new String[] { visitorName, caseStudyName }); //code for sending email try {//w w w .j ava2s . c o m InternetAddress fromAddress = new InternetAddress(emailAdd); InternetAddress toAddress = new InternetAddress(receiveEmailAdd); MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body, true); MailServiceUtil.sendEmail(mailMessage); } catch (AddressException e) { // TODO Auto-generated catch block _log.error("Error Sending Message", e); } finally { //For Downloading a resource String contentDisposition = "attachment; filename=" + caseStudyName + "." + resExtension; OutputStream out = resourceResponse.getPortletOutputStream(); //LInk for resource URL Goes here. Uncomment it out for dynamic location and comment out the static link URL url = new URL(resourceurl); URLConnection conn = url.openConnection(); resourceResponse.setContentType(conn.getContentType()); resourceResponse.setContentLength(conn.getContentLength()); resourceResponse.setCharacterEncoding(conn.getContentEncoding()); resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION, contentDisposition); resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600"); // open the stream and put it into BufferedReader InputStream stream = conn.getInputStream(); int c; while ((c = stream.read()) != -1) { out.write(c); } out.flush(); out.close(); stream.close(); } }
From source file:backend.SoftwareSecurity.java
private JSONObject connectToServer(String site) { //every call to this method must add to the blankjsonobjectmadetoavoiderrors if it calls a new json key try {/* ww w . j ava 2s .c o m*/ URLConnection con = new URL(site).openConnection(); con.setConnectTimeout(timeoutMS); con.setReadTimeout(timeoutMS); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); System.out.println("Server connection successful, returning JSON from server"); return new JSONObject(IOUtils.toString(in, encoding == null ? "UTF-8" : encoding)); } catch (Exception e) { System.out.println("Error during server connection: " + e.getMessage()); } JSONObject blankJSONObjectMadeToAvoidErrors = new JSONObject(); //returns json object with values expected to avoid org.jsonexception blankJSONObjectMadeToAvoidErrors.put("banned", "no"); blankJSONObjectMadeToAvoidErrors.put("version", 0.1); blankJSONObjectMadeToAvoidErrors.put("success", false); blankJSONObjectMadeToAvoidErrors.put("licenseType", ""); return blankJSONObjectMadeToAvoidErrors; //returns null if error connecting }
From source file:de.l3s.boilerpipe.sax.HTMLFetcher.java
public static HTMLDocument fetchHelper(final URL url) throws IOException { final URLConnection conn = url.openConnection(); //conn.setRequestProperty("User-Agent", //"Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36"); //conn.setRequestProperty("Cookie","wapparam=web2wap; vt=4"); final String ct = conn.getContentType(); if (ct == null || !(ct.equals("text/html") || ct.startsWith("text/html;"))) { //throw new IOException("Unsupported content type: "+ct+ url); System.err.println("WARN: unsupported Content-type: " + ct + url); }//from ww w.j a va2 s.c o m Charset cs = Charset.forName("UTF8"); if (ct != null) { Matcher m = PAT_CHARSET_REX.matcher(ct); if (m.find()) { final String charset = m.group(1); try { cs = Charset.forName(charset); } catch (UnsupportedCharsetException e) { // keep default } } } InputStream in = conn.getInputStream(); final String encoding = conn.getContentEncoding(); if (encoding != null) { if ("gzip".equalsIgnoreCase(encoding)) { in = new GZIPInputStream(in); } else { System.err.println("WARN: unsupported Content-Encoding: " + encoding); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int r; while ((r = in.read(buf)) != -1) { bos.write(buf, 0, r); } in.close(); final byte[] data = bos.toByteArray(); return new HTMLDocument(data, cs); }