List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:util.connection.AnonymousClient.java
public String download(URL source) throws IOException { // set the request URL URL request = new URL(source, source.getFile(), handler); // send request and receive response log.info("download (start) from source=" + source); URLConnection connection = request.openConnection(); connection.setDoOutput(true);/*from w w w .j a v a2 s. co m*/ connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.connect(); String responsebody = IOUtils.toString(connection.getInputStream(), "UTF-8"); // read the response netLayer.clear(); netLayer.waitUntilReady(); return responsebody; }
From source file:org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO.java
/** * Loads definitions from an URL without loading from "parent" URLs. * * @param url The URL to read./*from w ww .java2s . c om*/ * @return The definition map that has been read. */ protected Map<String, Definition> loadDefinitionsFromURL(URL url) { Map<String, Definition> defsMap = null; try { URLConnection connection = url.openConnection(); connection.connect(); lastModifiedDates.put(url.toExternalForm(), connection.getLastModified()); // Definition must be collected, starting from the base // source up to the last localized file. defsMap = reader.read(connection.getInputStream()); } catch (FileNotFoundException e) { // File not found. continue. if (log.isDebugEnabled()) { log.debug("File " + null + " not found, continue"); } } catch (IOException e) { throw new DefinitionsFactoryException("I/O error processing configuration.", e); } return defsMap; }
From source file:org.gradle.internal.resource.UriResource.java
private Reader getInputStream(URI url) throws IOException { final URLConnection urlConnection = url.toURL().openConnection(); urlConnection.setRequestProperty("User-Agent", getUserAgentString()); urlConnection.connect(); String charset = extractCharacterEncoding(urlConnection.getContentType(), "utf-8"); return new InputStreamReader(urlConnection.getInputStream(), charset); }
From source file:org.pentaho.reporting.libraries.resourceloader.loader.URLResourceData.java
public InputStream getResourceAsStream(final ResourceManager caller) throws ResourceLoadingException { try {//from w w w . j a va 2s . c om final URLConnection c = url.openConnection(); c.setDoOutput(false); c.setAllowUserInteraction(false); c.connect(); if (isFixBrokenWebServiceDateHeader() == false) { readMetaData(c); } return c.getInputStream(); } catch (IOException e) { throw new ResourceLoadingException("Failed to open URL connection", e); } }
From source file:org.jesterj.ingest.processors.FetchUrl.java
@Override public Document[] processDocument(Document document) { URL url = null;/* w w w . j a va2 s.com*/ try { url = new URL(document.getFirstValue(linkField)); String protocol = url.getProtocol(); String server = url.getHost(); Long lastAccess = visitedSiteCache.getIfPresent(server); long now = System.currentTimeMillis(); if (lastAccess == null) { visitedSiteCache.put(server, now); } else { long elapsed = now - lastAccess; if (elapsed < throttleMs) { try { Thread.sleep(throttleMs - elapsed); } catch (InterruptedException e) { // ignore, not really important. } } } URLConnection conn = url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (protocol != null && ("http".equals(protocol) || "https".equals(protocol))) { HttpURLConnection httpConnection = (HttpURLConnection) conn; int responseCode = httpConnection.getResponseCode(); if (httpStatusField != null) { document.put(httpStatusField, String.valueOf(responseCode)); } if (responseCode >= 400) { String message = "HTTP server responded " + responseCode + " " + httpConnection.getResponseMessage(); if (errorField != null) { document.put(errorField, message); } throw new IOException(message); } } IOUtils.copy(conn.getInputStream(), baos); document.setRawData(baos.toByteArray()); } catch (IOException e) { if (failOnIOError) { if (errorField != null) { document.put(errorField, e.getMessage()); } document.setStatus(Status.ERROR); } else { log.warn("Could not fetch " + url + " for " + document.getId(), e); } } return new Document[] { document }; }
From source file:io.v.positioning.gae.ServletPostAsyncTask.java
@Override protected String doInBackground(Context... params) { mContext = params[0];/*from w w w .ja va2 s . co m*/ DataOutputStream os = null; InputStream is = null; try { URLConnection conn = mUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.connect(); os = new DataOutputStream(conn.getOutputStream()); os.write(mData.toString().getBytes("UTF-8")); is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); return br.readLine(); } catch (IOException e) { return "IOException while contacting GEA: " + e.getMessage(); } catch (Exception e) { return "Exception while contacting GEA: " + e.getLocalizedMessage(); } finally { if (os != null) try { os.close(); } catch (IOException e) { return "IOException closing os: " + e.getMessage(); } if (is != null) try { is.close(); } catch (IOException e) { return "IOException closing is: " + e.getMessage(); } } }
From source file:org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO.java
/** {@inheritDoc} */ public boolean refreshRequired() { boolean status = false; Set<String> urls = lastModifiedDates.keySet(); try {//from ww w .j a v a 2 s . c o m for (String urlPath : urls) { Long lastModifiedDate = lastModifiedDates.get(urlPath); URL url = new URL(urlPath); URLConnection connection = url.openConnection(); connection.connect(); long newModDate = connection.getLastModified(); if (newModDate != lastModifiedDate) { status = true; break; } } } catch (Exception e) { log.warn("Exception while monitoring update times.", e); return true; } return status; }
From source file:at.alladin.rmbt.android.util.AsyncHtmlContentRetriever.java
@Override protected List<Object> doInBackground(String... params) { List<Object> result = new ArrayList<Object>(); URL url;/*from w w w. j ava2 s.com*/ URLConnection connection; try { url = new URL(params[0]); connection = url.openConnection(); connection.setConnectTimeout(3000); connection.connect(); } catch (Exception e) { result.add(-1); result.add(null); return result; } String htmlContent = ""; HttpGet httpGet = new HttpGet(params[0]); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response; int statusCode = -1; try { response = httpClient.execute(httpGet); statusCode = response.getStatusLine().getStatusCode(); System.out.println("response code: " + statusCode); /* * load HTML: */ /* HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = entity.getContent(); htmlContent = convertToString(inputStream); } */ } catch (Exception e) { result.add(-1); result.add(null); return result; } result.add(statusCode); result.add(htmlContent); return result; }
From source file:io.trivium.Registry.java
public void reload() { final String PREFIX = "META-INF/services/"; ClassLoader tvmLoader = ClassLoader.getSystemClassLoader(); //types// w w w.j a va 2 s . c om try { Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Fact"); while (resUrl.hasMoreElements()) { URL url = resUrl.nextElement(); URLConnection connection = url.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); List<String> lines = IOUtils.readLines(is, "UTF-8"); is.close(); for (String line : lines) { Class<? extends Fact> clazz = (Class<? extends Fact>) Class.forName(line); Fact prototype = clazz.newInstance(); if (!types.containsKey(prototype.getTypeRef())) { types.put(prototype.getTypeRef(), clazz); } logger.log(Level.FINE, "registered type {0}", prototype.getFactName()); } } } catch (Exception ex) { logger.log(Level.SEVERE, "dynamically loading types failed", ex); } //bindings try { Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Binding"); while (resUrl.hasMoreElements()) { URL url = resUrl.nextElement(); URLConnection connection = url.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); List<String> lines = IOUtils.readLines(is, "UTF-8"); is.close(); for (String line : lines) { Class<? extends Binding> clazz = (Class<? extends Binding>) Class.forName(line); Binding prototype = clazz.newInstance(); if (!bindings.containsKey(prototype.getTypeRef())) { bindings.put(prototype.getTypeRef(), clazz); //register prototype bindingInstances.put(prototype.getTypeRef(), prototype); } logger.log(Level.FINE, "registered binding {0}", prototype.getName()); } } } catch (Exception ex) { logger.log(Level.SEVERE, "dynamically loading bindings failed", ex); } //tasks try { Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Task"); while (resUrl.hasMoreElements()) { URL url = resUrl.nextElement(); URLConnection connection = url.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); List<String> lines = IOUtils.readLines(is, "UTF-8"); is.close(); for (String line : lines) { Class<? extends Task> clazz = (Class<? extends Task>) Class.forName(line); Task prototype = clazz.newInstance(); if (!tasks.containsKey(prototype.getTypeRef())) { tasks.put(prototype.getTypeRef(), clazz); } logger.log(Level.FINE, "registered binding {0}", prototype.getName()); } } } catch (Exception ex) { logger.log(Level.SEVERE, "dynamically loading bindings failed", ex); } //testcases try { Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.test.TestCase"); while (resUrl.hasMoreElements()) { URL url = resUrl.nextElement(); URLConnection connection = url.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); List<String> lines = IOUtils.readLines(is, "UTF-8"); is.close(); for (String line : lines) { Class<? extends TestCase> clazz = (Class<? extends TestCase>) Class.forName(line); TestCase prototype = clazz.newInstance(); if (!testcases.containsKey(prototype.getTypeRef())) { testcases.put(prototype.getTypeRef(), prototype); } logger.log(Level.FINE, "registered testcase {0}", prototype.getTypeRef()); } } } catch (Exception ex) { logger.log(Level.SEVERE, "dynamically loading test cases failed", ex); } }
From source file:org.squale.welcom.struts.webServer.URLManager.java
/** * Recupere la date du fichier dont le chemin est l'url * /*from w w w. j a v a 2 s.c om*/ * @param pUrl : L'url * @return : Date dir URL OK * @throws IOException Probleme sur l'ouverture de la connection */ public Date getURLDate(final URL pUrl) throws IOException { final URLConnection urlcon = pUrl.openConnection(); urlcon.setUseCaches(true); urlcon.connect(); return new Date(urlcon.getLastModified()); }