List of usage examples for java.net HttpURLConnection disconnect
public abstract void disconnect();
From source file:forge.download.GuiDownloadService.java
@Override public void run() { Proxy p = getProxy();//from ww w .ja v a 2s . c om int bufferLength; int iCard = 0; byte[] buffer = new byte[1024]; for (Entry<String, String> kv : files.entrySet()) { if (cancel) { break; } String url = kv.getValue(); final File fileDest = new File(kv.getKey()); final File base = fileDest.getParentFile(); FileOutputStream fos = null; try { // test for folder existence if (!base.exists() && !base.mkdir()) { // create folder if not found System.out.println("Can't create folder" + base.getAbsolutePath()); } URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(p); // don't allow redirections here -- they indicate 'file not found' on the server conn.setInstanceFollowRedirects(false); conn.connect(); if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { conn.disconnect(); System.out.println(url); System.out.println("Skipped Download for: " + fileDest.getPath()); update(++iCard, fileDest); skipped++; continue; } fos = new FileOutputStream(fileDest); InputStream inputStream = conn.getInputStream(); while ((bufferLength = inputStream.read(buffer)) > 0) { fos.write(buffer, 0, bufferLength); } } catch (final ConnectException ce) { System.out.println("Connection refused for url: " + url); } catch (final MalformedURLException mURLe) { System.out.println("Error - possibly missing URL for: " + fileDest.getName()); } catch (final FileNotFoundException fnfe) { String formatStr = "Error - the LQ picture %s could not be found on the server. [%s] - %s"; System.out.println(String.format(formatStr, fileDest.getName(), url, fnfe.getMessage())); } catch (final Exception ex) { Log.error("LQ Pictures", "Error downloading pictures", ex); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("error closing output stream"); } } } update(++iCard, fileDest); } }
From source file:eu.liveandgov.ar.utilities.Download_Data.java
/** DownAndCopy * /* w ww. jav a2s . c o m*/ * Download file from URL and Copy to Android file system folder * * @param fileUrl * @param StringAndroidPath */ public static boolean DownAndCopy(String fileUrlSTR, String StringAndroidPath, boolean preservefilename, String Caller) { if (compareRemoteWithLocal(fileUrlSTR, StringAndroidPath)) { //Log.e("fileUrlSTR", "SKIP WITH HASH"); return true; } else { Log.e("TRY TO DOWNLOAD BY " + Caller, fileUrlSTR); } SimpleDateFormat sdf = new SimpleDateFormat("mm"); // Check if downloaded at least just 2 minutes ago for (String[] mem : MemDown) if (fileUrlSTR.equals(mem[0])) { int diff = Integer.parseInt(sdf.format(new Date())) - Integer.parseInt(mem[1]); Log.e("diff", " " + diff); if (diff < 2) { Log.d("Download_Data", "I am not downloading " + fileUrlSTR + " because it was downloaded " + diff + " minutes ago"); return true; } } if (!OS_Utils.exists(fileUrlSTR)) { Log.e("Download_Data", "URL: " + fileUrlSTR + " called from " + Caller + " not exists to copy it to " + StringAndroidPath); return false; } int DEBUG_FLAG = 0; HttpURLConnection conn; URL fileUrl = null; try { fileUrl = new URL(fileUrlSTR); } catch (MalformedURLException e1) { return false; } try { conn = (HttpURLConnection) fileUrl.openConnection(); DEBUG_FLAG = 1; conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(100); DEBUG_FLAG = 2; int current = 0; byte[] buffer = new byte[10]; while ((current = bis.read(buffer)) != -1) baf.append(buffer, 0, current); DEBUG_FLAG = 3; /* Convert the Bytes read to a String. */ File fileAndroid; try { if (preservefilename) { int iSlash = fileUrlSTR.lastIndexOf("/"); fileAndroid = new File(StringAndroidPath + "/" + fileUrlSTR.substring(iSlash + 1)); } else fileAndroid = new File(StringAndroidPath); } catch (Exception e) { Log.e("Download_Data.DownAndCopy", "I can not create " + StringAndroidPath); bis.close(); conn.disconnect(); return false; } DEBUG_FLAG = 4; FileOutputStream fos = new FileOutputStream(fileAndroid); fos.write(baf.toByteArray()); DEBUG_FLAG = 5; bis.close(); fos.close(); conn.disconnect(); MemDown.add(new String[] { fileUrlSTR, sdf.format(new Date()) }); return true; //returns including the filename } catch (IOException e) { Log.e("Download_Data", "Download_Data: Error when trying to download: " + fileUrl.toString() + " to " + StringAndroidPath + " DEBUG_FLAG=" + DEBUG_FLAG); return false; } }
From source file:io.github.retz.web.JobRequestRouter.java
private static String fetchHTTP(String addr, int retry) throws MalformedURLException, IOException { LOG.debug("Fetching {}", addr); HttpURLConnection conn = null; try {/*w w w . j a va 2 s. co m*/ conn = (HttpURLConnection) new URL(addr).openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); LOG.debug(conn.getResponseMessage()); } catch (MalformedURLException e) { LOG.error(e.toString()); throw e; } catch (IOException e) { LOG.error(e.toString()); throw e; } try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8))) { StringBuilder builder = new StringBuilder(); String line; do { line = reader.readLine(); builder.append(line); } while (line != null); LOG.debug("Fetched {} bytes from {}", builder.toString().length(), addr); return builder.toString(); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { // Somehow this happens even HTTP was correct LOG.debug("Cannot fetch file {}: {}", addr, e.toString()); // Just retry until your stack get stuck; thanks to SO:33340848 // and to that crappy HttpURLConnection if (retry < 0) { LOG.error("Retry failed. Last error was: {}", e.toString()); throw e; } return fetchHTTP(addr, retry - 1); } finally { conn.disconnect(); } }
From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java
/** * @param src of image to download and save * @param folder to which to save the image * @param fileName to use for the saved image *///from w ww . ja v a2 s . c o m protected final void saveImage(String src, String folder, String fileName) { if (fileName.contains("?")) { fileName = fileName.substring(0, fileName.lastIndexOf("?")); } fileName = fileName.replaceAll("[^a-zA-Z0-9.-]", "_"); // replace non valid file fileName characters File outputFile = new File(folder + "\\" + fileName); try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"); connection.setRequestMethod("HEAD"); connection.setInstanceFollowRedirects(false); int rspCode = connection.getResponseCode(); if (rspCode == 301) { // redirected, so get new url String newUrl = connection.getHeaderField("Location"); url = new URL(newUrl); } connection.disconnect(); FileUtils.copyURLToFile(url, outputFile); } catch (MalformedURLException e) { logger.error("Malformed url exception for image src: " + src, e); } catch (IOException e) { logger.error("IO exception for saving image src locally: " + src, e); } }
From source file:fr.gael.dhus.datastore.scanner.ODataScanner.java
@Override public int scan() throws InterruptedException { // Workaround: transferring 1 product. see l.50 if (this.client == null) { try {// w w w . j a va2 s . c o m URL url = new URL(this.uri); HttpURLConnection co = (HttpURLConnection) url.openConnection(); // HTTP Basic Authentication. String userpass = this.username + ":" + this.password; String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes())); co.setRequestProperty("Authorization", basicAuth); co.connect(); String cd = co.getHeaderField("Content-Disposition"); co.disconnect(); Matcher m = Pattern.compile(".*?filename=\"(.*?)\\.zip\".*?").matcher((cd)); if (!m.matches()) throw new Exception("filename not in header"); String filename = m.group(1); //url = new URIBuilder (url.toURI ()) // .setUserInfo (this.username, this.password).build ().toURL (); this.getScanList().add(new URLExt(url, false, filename)); } catch (Exception e) { LOGGER.error("failed to transfer " + this.uri, e); return 0; } return 1; } // The actual scan() code. int res = 0; try { // Prepare OData request, we want Products. String resource_path = new String("/Products"); Facets facets = (new Facets()).setNullable(false); Map<String, String> query_params = new HashMap<>(); long now = System.currentTimeMillis(); EdmSimpleType type = RuntimeDelegate.getEdmSimpleType(DateTime); // Filtering by ingestionDate. if (this.lastScanTime != 0L) { StringBuilder sb = new StringBuilder("IngestionDate ge "); Date l_time = new Date(this.lastScanTime); sb.append(type.valueToString(l_time, EdmLiteralKind.URI, facets)); sb.append(" and IngestionDate lt "); l_time = new Date(now); sb.append(type.valueToString(l_time, EdmLiteralKind.URI, facets)); query_params.put("$filter", sb.toString()); } else { Date l_time = new Date(now); String value = "IngestionDate lt " + type.valueToString(l_time, EdmLiteralKind.URI, facets); query_params.put("$filter", value); } // Ordering by ingestionDate. query_params.put("$orderby", "IngestionDate"); // Pagination. int page_len = 50; // TODO get this value from config. query_params.put("$top", String.valueOf(page_len)); try { for (long i = 0;; i++) { if (i != 0) query_params.put("$skip", String.valueOf(page_len * i)); ODataFeed of = this.client.readFeed(resource_path, query_params); for (ODataEntry entry : of.getEntries()) { String pdt_name = (String) entry.getProperties().get("Name"); if (this.getUserPattern() == null || this.getUserPattern().matcher(pdt_name).matches()) { String key = (String) entry.getProperties().get("Id"); URL url = new URIBuilder( this.client.getServiceRoot() + "/Products" + "('" + key + "')/$value") .setUserInfo(this.username, this.password).build().toURL(); this.getScanList().add(new URLExt(url, false, pdt_name)); res += 1; } } if (of.getEntries().size() != page_len) // End of pagination. break; } } catch (ODataException | IOException | URISyntaxException e) { LOGGER.error("Product retrieval failed", e); } this.lastScanTime = now; } catch (ODataException e) { LOGGER.error(e); } return res; }
From source file:org.devnexus.aerogear.HttpRestProvider.java
/** * {@inheritDoc}// ww w .j av a 2s . c om */ @Override public HeaderAndBody delete(String id) throws RuntimeException { HttpURLConnection urlConnection = null; try { urlConnection = prepareConnection(id); urlConnection.setRequestMethod("DELETE"); return getHeaderAndBody(urlConnection); } catch (IOException e) { Log.e(TAG, "Error on DELETE of " + url, e); throw new RuntimeException(e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:net.kevxu.muzei.interfacelift.InterfaceliftMacdropsClient.java
protected String fetchPlainText(String query) throws IOException { URL url = new URL(query); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", getUserAgent()); try {/*from w w w . ja v a2 s .co m*/ InputStream in = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } return new String(out.toByteArray(), "UTF-8"); } finally { connection.disconnect(); } }
From source file:it.iit.genomics.cru.structures.bridges.pdb.PDBWSClient.java
/** * * @param pdbIds/* w w w . j a va 2s. c o m*/ * @return * @throws BridgesRemoteAccessException */ public MoleculeDescription getDescription(Collection<String> pdbIds) throws BridgesRemoteAccessException { try { URL url = new URL(pdbUrl + "/describeMol?structureId=" + StringUtils.join(pdbIds, ",")); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/xml"); if (conn.getResponseCode() != 200) { throw new BridgesRemoteAccessException("Failed : HTTP error code : " + conn.getResponseCode()); } MoleculeDescription result = parseMoleculeDescription(conn.getInputStream()); conn.disconnect(); return result; } catch (IOException e) { logger.error("Fail to get PDB files for {0}", StringUtils.join(pdbIds, ", there may be a network problem.")); throw new BridgesRemoteAccessException("Failed access to PDB REST service"); } }
From source file:it.polito.tellmefirst.apimanager.RestManager.java
public String getStringFromAPI(String urlStr) { LOG.debug("[getStringFromAPI] - BEGIN - url=" + urlStr); String result = ""; try {// ww w . j a v a 2 s.c om LOG.debug("[getStringFromAPI] - http.proxyHost=" + System.getProperty("http.proxyHost")); LOG.debug("[getStringFromAPI] - http.proxyPort=" + System.getProperty("http.proxyPort")); LOG.debug("[getStringFromAPI] - https.proxyHost=" + System.getProperty("https.proxyHost")); LOG.debug("[getStringFromAPI] - https.proxyPort=" + System.getProperty("https.proxyPort")); LOG.debug("[getStringFromAPI] - http.nonProxyHosts=" + System.getProperty("http.nonProxyHosts")); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15000); //set timeout to 15 seconds conn.setReadTimeout(15000); //set timeout to 15 seconds if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); result = sb.toString(); } catch (Exception e) { LOG.error("[getStringFromAPI] - EXCEPTION for url=" + urlStr, e); } LOG.debug("[getStringFromAPI] - END"); return result; }
From source file:CSV.HandlingCSV.java
public boolean verifyConnectUrl() { HttpURLConnection connection = null; try {// w ww . ja v a 2 s . c o m URL u = new URL(this.url); connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("HEAD"); int code = connection.getResponseCode(); return true; } catch (MalformedURLException e) { return false; } catch (IOException e) { return false; } finally { if (connection != null) { connection.disconnect(); } } }