List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:sce.RESTAppMetricJob.java
public static byte[] readURL(String url) { try {//from w w w .j a va 2s. co m URL u = new URL(url); URLConnection conn = u.openConnection(); // look like simulating the request coming from Web browser solve 403 error conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); byte[] bytes; try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) { String json = in.readLine(); bytes = json.getBytes("UTF-8"); } return bytes; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:jfix.util.Urls.java
/** * Returns content from given url as string. The url can contain * username:password after the protocol, so that basic authorization is * possible.//w w w .j a v a 2 s . c o m * * Example for url with basic authorization: * * http://username:password@www.domain.org/index.html */ public static String readString(String url, int timeout) { Reader reader = null; try { URLConnection uc = new URL(url).openConnection(); if (uc instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) uc; httpConnection.setConnectTimeout(timeout * 1000); httpConnection.setReadTimeout(timeout * 1000); } Matcher matcher = Pattern.compile("://(\\w+:\\w+)@").matcher(url); if (matcher.find()) { String auth = matcher.group(1); String encoding = Base64.getEncoder().encodeToString(auth.getBytes()); uc.setRequestProperty("Authorization", "Basic " + encoding); } String charset = (uc.getContentType() != null && uc.getContentType().contains("charset=")) ? uc.getContentType().split("charset=")[1] : "utf-8"; reader = new BufferedReader(new InputStreamReader(uc.getInputStream(), charset)); StringBuilder sb = new StringBuilder(); for (int chr; (chr = reader.read()) != -1;) { sb.append((char) chr); } return sb.toString(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } } }
From source file:de.dal33t.powerfolder.util.ConfigurationLoader.java
/** * Loads a pre-configuration from the URL * * @param from//ww w . j a v a2 s .c o m * the URL to load from * @return the loaded properties WITHOUT those in config. * @throws IOException */ private static Properties loadPreConfiguration(URL from, String un, char[] pw) throws IOException { Reject.ifNull(from, "URL is null"); URLConnection con = from.openConnection(); if (StringUtils.isNotBlank(un)) { String s = un + ":" + Util.toString(pw); LoginUtil.clear(pw); String base64 = "Basic " + Base64.encodeBytes(s.getBytes("UTF-8")); con.setRequestProperty("Authorization", base64); } con.setConnectTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS); con.setReadTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS); con.connect(); InputStream in = con.getInputStream(); try { return loadPreConfiguration(in); } finally { try { in.close(); } catch (Exception e) { } } }
From source file:org.eclipse.wst.ws.internal.parser.discovery.NetUtils.java
/** * Get the java.net.URLConnection given a string representing the URL. This class ensures * that proxy settings in WSAD are respected. * @param urlString String representing the URL. * @return java.net.URLCDonnection URLConnection to the URL. */// w w w . java 2s . c om public static final URLConnection getURLConnection(String urlString) { try { URL url = createURL(urlString); URLConnection uc = url.openConnection(); String proxyUserName = System.getProperty("http.proxyUserName"); String proxyPassword = System.getProperty("http.proxyPassword"); if (proxyUserName != null && proxyPassword != null) { StringBuffer userNamePassword = new StringBuffer(proxyUserName); userNamePassword.append(':').append(proxyPassword); Base64 encoder = new Base64(); String encoding = new String(encoder.encode(userNamePassword.toString().getBytes())); userNamePassword.setLength(0); userNamePassword.append("Basic ").append(encoding); uc.setRequestProperty("Proxy-authorization", userNamePassword.toString()); } return uc; } catch (MalformedURLException e) { } catch (IOException e) { } return null; }
From source file:org.drugis.addis.imports.ClinicaltrialsImporter.java
static void getClinicaltrialsData(Study study, String url, boolean importResults) throws IOException { URLConnection conn = new URL(url).openConnection(); conn.setRequestProperty("Accept", "application/xml"); getClinicaltrialsData(study, conn.getInputStream(), importResults); }
From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java
@SuppressWarnings("restriction") public static String readStringFromUrl(String url, String username, String password) throws IOException { URLConnection connection = new URL(url).openConnection(); connection.setConnectTimeout(5000);// w w w . j a va2 s. co m connection.setReadTimeout(5000); connection.setDoOutput(true); if (username != null) { String userpass = username + ":" + password; // TODO: Use apache commons instead. String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); connection.setRequestProperty("Authorization", basicAuth); } InputStream in = connection.getInputStream(); try { return IOUtils.toString(in, "UTF-8"); } finally { in.close(); } }
From source file:com.chiorichan.util.WebUtils.java
public static byte[] readUrlWithException(String surl, String user, String pass) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); URL url = new URL(surl); URLConnection uc = url.openConnection(); if (user != null || pass != null) { String userpass = user + ":" + pass; String basicAuth = "Basic " + new String(Base64.encodeBase64(userpass.getBytes())); uc.setRequestProperty("Authorization", basicAuth); }// w ww . j a v a 2 s .co m InputStream is = uc.getInputStream(); byte[] byteChunk = new byte[4096]; int n; while ((n = is.read(byteChunk)) > 0) { out.write(byteChunk, 0, n); } is.close(); return out.toByteArray(); }
From source file:org.jboss.tools.ws.ui.utils.NetUtils.java
/** * Get the java.net.URLConnection given a string representing the URL. This class ensures * that proxy settings in WSAD are respected. * @param urlString String representing the URL. * @return java.net.URLCDonnection URLConnection to the URL. *///from w ww . java 2 s . co m public static final URLConnection getURLConnection(String urlString) { try { URL url = createURL(urlString); URLConnection uc = url.openConnection(); String proxyUserName = System.getProperty("http.proxyUserName"); //$NON-NLS-1$ String proxyPassword = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ if (proxyUserName != null && proxyPassword != null) { StringBuffer userNamePassword = new StringBuffer(proxyUserName); userNamePassword.append(':').append(proxyPassword); Base64 encoder = new Base64(); String encoding = new String(encoder.encode(userNamePassword.toString().getBytes())); userNamePassword.setLength(0); userNamePassword.append("Basic ").append(encoding); //$NON-NLS-1$ uc.setRequestProperty("Proxy-authorization", userNamePassword.toString()); //$NON-NLS-1$ } return uc; } catch (MalformedURLException e) { } catch (IOException e) { } return null; }
From source file:ubic.basecode.ontology.OntologyLoader.java
/** * Load an ontology into memory. Use this type of model when fast access is critical and memory is available. * If load from URL fails, attempt to load from disk cache under @cacheName. * /*from w w w . j a va 2 s.c o m*/ * @param url * @param spec e.g. OWL_MEM_TRANS_INF * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection * @return */ public static OntModel loadMemoryModel(String url, OntModelSpec spec, String cacheName) { StopWatch timer = new StopWatch(); timer.start(); OntModel model = getMemoryModel(url, spec); URLConnection urlc = null; int tries = 0; while (tries < MAX_CONNECTION_TRIES) { try { urlc = new URL(url).openConnection(); // help ensure mis-configured web servers aren't causing trouble. urlc.setRequestProperty("Accept", "application/rdf+xml"); try { HttpURLConnection c = (HttpURLConnection) urlc; c.setInstanceFollowRedirects(true); } catch (ClassCastException e) { // not via http, using a FileURLConnection. } if (tries > 0) { log.info("Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES + " of max tries"); } else { log.info("Connecting to " + url); } urlc.connect(); // Will error here on bad URL if (urlc instanceof HttpURLConnection) { String newUrl = urlc.getHeaderField("Location"); if (StringUtils.isNotBlank(newUrl)) { log.info("Redirect to " + newUrl); urlc = new URL(newUrl).openConnection(); // help ensure mis-configured web servers aren't causing trouble. urlc.setRequestProperty("Accept", "application/rdf+xml"); urlc.connect(); } } break; } catch (IOException e) { // try to recover. log.error(e + " retrying?"); tries++; } } if (urlc != null) { try (InputStream in = urlc.getInputStream();) { Reader reader; if (cacheName != null) { // write tmp to disk File tempFile = getTmpDiskCachePath(cacheName); if (tempFile == null) { reader = new InputStreamReader(in); } else { tempFile.getParentFile().mkdirs(); Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); reader = new FileReader(tempFile); } } else { // Skip the cache reader = new InputStreamReader(in); } assert reader != null; try (BufferedReader buf = new BufferedReader(reader);) { model.read(buf, url); } log.info("Load model: " + timer.getTime() + "ms"); } catch (IOException e) { log.error(e.getMessage(), e); } } if (cacheName != null) { File f = getDiskCachePath(cacheName); File tempFile = getTmpDiskCachePath(cacheName); File oldFile = getOldDiskCachePath(cacheName); if (model.isEmpty()) { // Attempt to load from disk cache if (f == null) { throw new RuntimeException( "Ontology cache directory required to load from disk: ontology.cache.dir"); } if (f.exists() && !f.isDirectory()) { try (BufferedReader buf = new BufferedReader(new FileReader(f));) { model.read(buf, url); // We successfully loaded the cached ontology. Copy the loaded ontology to oldFile // so that we don't recreate indices during initialization based on a false change in // the ontology. Files.copy(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING); log.info("Load model from disk: " + timer.getTime() + "ms"); } catch (IOException e) { log.error(e.getMessage(), e); throw new RuntimeException( "Ontology failed load from URL (" + url + ") and disk cache: " + cacheName); } } else { throw new RuntimeException("Ontology failed load from URL (" + url + ") and disk cache does not exist: " + cacheName); } } else { // Model was successfully loaded into memory from URL with given cacheName // Save cache to disk (rename temp file) log.info("Caching ontology to disk: " + cacheName); if (f != null) { try { // Need to compare previous to current so instead of overwriting we'll move the old file f.createNewFile(); Files.move(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.move(tempFile.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { log.error(e.getMessage(), e); } } else { log.warn("Ontology cache directory required to save to disk: ontology.cache.dir"); } } } assert !model.isEmpty(); return model; }
From source file:org.wso2.carbon.cloud.gateway.agent.CGAgentUtils.java
private static URLConnection getURLConnection(URL url) throws CGException { URLConnection connection; if (url.getProtocol().equalsIgnoreCase("https")) { String msg = "Connecting through doesn't support"; log.error(msg);//from w w w. j a v a 2 s . co m throw new CGException(msg); } else { try { connection = url.openConnection(); } catch (IOException e) { throw new CGException("Could not open the URL connection", e); } } connection.setReadTimeout(getReadTimeout()); connection.setConnectTimeout(getConnectTimeout()); connection.setRequestProperty("Connection", "close"); // if http is being used return connection; }