List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:radet.publisher.JmsPublishJob.java
private static boolean downloadPdfIfModifiedSince(long modifiedSince) throws IOException, MalformedURLException { SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); Calendar ifModifiedSince = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US); ifModifiedSince.setTimeInMillis(modifiedSince); URL url = new URL("http://radet.ro/opriri/Opriri_programate.pdf"); URLConnection connection = url.openConnection(); connection.setRequestProperty("If-Modified-Since", sdf.format(ifModifiedSince.getTime())); try (InputStream urlInputStream = connection.getInputStream()) { try (OutputStream pdfOutputStream = new FileOutputStream(PDF_FILE)) { IOUtils.write(IOUtils.toByteArray(urlInputStream), pdfOutputStream); }/*from w w w . jav a2s .c om*/ } return connection.getContentLengthLong() > 0; }
From source file:net.foxgenesis.helper.SiteReader.java
private static BufferedReader getStream(URL url) throws IOException { URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", "X-ServerStats"); connection.setReadTimeout(5000);//from w w w . j a va2 s. co m return new BufferedReader(new InputStreamReader(connection.getInputStream())); }
From source file:Utils.RdwTool.java
/** * * @param numberplate a dutch license plate. * @return//w ww . j ava 2 s. co m * @throws IOException */ public static String getCarJSON(String licensePlate) throws IOException { //clean the license plate string if non alphanumeric characters. licensePlate = cleanLicensePlate(licensePlate); //string for connecting to the RDW API. //returned fields are determined in the $select clause. See for additional fields the respective documentation at the azure datamarket. //we're filtering on license plate in $filter. the license plate is provided by the user. String dmUrl = "https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/" + "KENT_VRTG_O_DAT?$format=json&$select=Aantalzitplaatsen,Handelsbenaming,Merk&$filter=Kenteken%20eq%20%27" + licensePlate + "%27"; //setting up the correct format for the accountkey String accountKey = "vRedoLtl8GWiKh3lKokeXLitDylXpWkrAlKIkAZPc/k"; byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes()); String accountKeyEnc = new String(accountKeyBytes); //connect to the URL with correct properties URL url = new URL(dmUrl); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc); //write the JSON reply to a string and return it BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder sb = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } String jsonText = sb.toString(); return jsonText; }
From source file:com.meetingninja.csse.database.BaseDatabaseAdapter.java
protected static void addRequestHeader(URLConnection connection, boolean isPost) { connection.setRequestProperty("User-Agent", USER_AGENT); connection.setRequestProperty("Accept", ACCEPT_TYPE); connection.setDoOutput(isPost);// w ww .j a v a2 s . c o m if (isPost) { connection.setRequestProperty("Content-Type", CONTENT_TYPE); } }
From source file:biz.wolschon.finance.jgnucash.helper.HttpFetcher.java
/** * Fetch the given URL with http-basic-auth. * @param url the URL/*from ww w . jav a2s.c o m*/ * @param username username * @param password password * @return the page-content or null */ public static InputStream fetchURLStream(final URL url, final String username, final String password) { try { String userPassword = username + ":" + password; // Encode String String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); URLConnection uc = url.openConnection(); uc.setRequestProperty("Authorization", "Basic " + encoding); return uc.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); LOGGER.error("cannot fetch malformed URL " + url, e); return null; } catch (IOException e) { e.printStackTrace(); LOGGER.error("cannot fetch URL " + url, e); return null; } }
From source file:com.vaadin.tools.ReportUsage.java
private static void doHttpGet(String userAgent, String url) { Throwable caught;/* ww w . jav a 2 s . c o m*/ InputStream is = null; try { URL urlToGet = new URL(url); URLConnection conn = urlToGet.openConnection(); conn.setRequestProperty(USER_AGENT, userAgent); is = conn.getInputStream(); // TODO use the results IOUtils.toByteArray(is); return; } catch (MalformedURLException e) { caught = e; } catch (IOException e) { caught = e; } finally { IOUtils.closeQuietly(is); } Logger.getLogger(ReportUsage.class.getName()) .fine("Caught an exception while executing HTTP query: " + caught.getMessage()); }
From source file:net.technicpack.launchercore.restful.RestObject.java
public static <T extends RestObject> T getRestObject(Class<T> restObject, String url) throws RestfulAPIException { InputStream stream = null;// ww w.ja v a2s. c om try { URLConnection conn = new URL(url).openConnection(); conn.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"); conn.setConnectTimeout(15000); conn.setReadTimeout(15000); stream = conn.getInputStream(); String data = IOUtils.toString(stream); T result = gson.fromJson(data, restObject); if (result == null) { throw new RestfulAPIException("Unable to access URL [" + url + "]"); } if (result.hasError()) { throw new RestfulAPIException("Error in response: " + result.getError()); } return result; } catch (SocketTimeoutException e) { throw new RestfulAPIException("Timed out accessing URL [" + url + "]", e); } catch (MalformedURLException e) { throw new RestfulAPIException("Invalid URL [" + url + "]", e); } catch (JsonParseException e) { throw new RestfulAPIException("Error parsing response JSON at URL [" + url + "]", e); } catch (IOException e) { throw new RestfulAPIException("Error accessing URL [" + url + "]", e); } finally { IOUtils.closeQuietly(stream); } }
From source file:org.fusesource.cloudmix.agent.security.SecurityUtils.java
/** * Get input stream for URL adding credentials if neccessary. * //from ww w .j av a2 s.c om * @param url * @return input stream for URL * @throws IOException */ public static InputStream getInputStream(URL url) throws IOException { URLConnection conn = url.openConnection(); String userInfo = url.getUserInfo(); if (userInfo != null && !"".equals(userInfo)) { conn.setRequestProperty("Authorization", getBasicAuthHeader(userInfo)); } return conn.getInputStream(); }
From source file:sx.blah.discord.util.Image.java
/** * Generates an avatar image from a direct link to an image. * * @param imageType The image type, ex. jpeg, png, etc. * @param url The direct link to an image. * @return The avatar image.//from w w w.ja v a 2 s.c o m */ static Image forUrl(String imageType, String url) { return () -> { try { URLConnection urlConnection = new URL(url).openConnection(); urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); InputStream stream = urlConnection.getInputStream(); return forStream(imageType, stream).getData(); } catch (IOException e) { Discord4J.LOGGER.error(LogMarkers.UTIL, "Discord4J Internal Exception", e); } return defaultAvatar().getData(); }; }
From source file:Bing.java
/** * * @param query - query to use for the search * @return - a json array of results. each contains a title, description, url, * and some other metadata that can be easily extracted since its in json format */// www .j a va 2 s . co m public static JSONArray search(String query) { try { query = "'" + query + "'"; String encodedQuery = URLEncoder.encode(query, "UTF-8"); System.out.println(encodedQuery); // URL url = new URL("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27car%27"); // URL url = new URL("http://csb.stanford.edu/class/public/pages/sykes_webdesign/05_simple.html"); String webPage = "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=" + encodedQuery + "&$format=JSON"; String name = "6604d12c-3e89-4859-8013-3132f78c1595"; String password = "cefgNRl3OL4PrJJvssxkqLw0VKfYNCgyTe8wNXotUmQ"; String authString = name + ":" + password; System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); System.out.println("Base64 encoded auth string: " + authStringEnc); URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine).append("\n"); } in.close(); JSONParser parser = new JSONParser(); JSONArray arr = (JSONArray) ((JSONObject) ((JSONObject) parser.parse(response.toString())).get("d")) .get("results"); JSONObject obj = (JSONObject) arr.get(0); JSONArray out = (JSONArray) obj.get("Web"); return out; // } catch (MalformedURLException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } return null; }