List of usage examples for java.net URLConnection setUseCaches
public void setUseCaches(boolean usecaches)
From source file:info.mallmc.framework.util.ProfileLoader.java
private void addProperties(GameProfile profile) { String uuid = getUUID(skinOwner); try {/* www . java 2 s.c om*/ // Get the name from SwordPVP URL url = new URL( "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"); URLConnection uc = url.openConnection(); uc.setUseCaches(false); uc.setDefaultUseCaches(false); uc.addRequestProperty("User-Agent", "Mozilla/5.0"); uc.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate"); uc.addRequestProperty("Pragma", "no-cache"); // Parse it String json = new Scanner(uc.getInputStream(), "UTF-8").useDelimiter("\\A").next(); JSONParser parser = new JSONParser(); Object obj = parser.parse(json); JSONArray properties = (JSONArray) ((JSONObject) obj).get("properties"); for (int i = 0; i < properties.size(); i++) { try { JSONObject property = (JSONObject) properties.get(i); String name = (String) property.get("name"); String value = (String) property.get("value"); String signature = property.containsKey("signature") ? (String) property.get("signature") : null; if (signature != null) { profile.getProperties().put(name, new Property(name, value, signature)); } else { profile.getProperties().put(name, new Property(value, name)); } } catch (Exception e) { L.ogError(LL.ERROR, Trigger.LOAD, "Failed to apply auth property", "Failed to apply auth property"); } } } catch (Exception e) { ; // Failed to load skin } }
From source file:net.sf.zekr.engine.network.NetworkController.java
public InputStream openSteam(String uri, int timeout) throws URISyntaxException, IOException { URL url = new URL(uri); URLConnection conn = url.openConnection(getProxy(uri)); conn.setReadTimeout(timeout);/*from w w w. j a v a 2 s . co m*/ conn.setUseCaches(true); return conn.getInputStream(); }
From source file:XMLResourceBundleControl.java
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) { throw new NullPointerException(); }/*from w ww . j av a 2s . co m*/ ResourceBundle bundle = null; if (!format.equals(XML)) { return null; } String bundleName = toBundleName(baseName, locale); String resourceName = toResourceName(bundleName, format); URL url = loader.getResource(resourceName); if (url == null) { return null; } URLConnection connection = url.openConnection(); if (connection == null) { return null; } if (reload) { connection.setUseCaches(false); } InputStream stream = connection.getInputStream(); if (stream == null) { return null; } BufferedInputStream bis = new BufferedInputStream(stream); bundle = new XMLResourceBundle(bis); bis.close(); return bundle; }
From source file:CounterApp.java
public int getCount() throws Exception { java.net.URL url = new java.net.URL(servletURL); java.net.URLConnection con = url.openConnection(); if (sessionCookie != null) { con.setRequestProperty("cookie", sessionCookie); }/*from w w w . j a v a 2 s .c o m*/ con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); DataInputStream in = new DataInputStream(con.getInputStream()); int count = in.readInt(); in.close(); if (sessionCookie == null) { String cookie = con.getHeaderField("set-cookie"); if (cookie != null) { sessionCookie = parseCookie(cookie); System.out.println("Setting session ID=" + sessionCookie); } } return count; }
From source file:io.v.positioning.gae.ServletPostAsyncTask.java
@Override protected String doInBackground(Context... params) { mContext = params[0];/*from w w w . j a va 2 s . c o 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.silverpeas.calendar.ServletConnector.java
/** * Open a connection to the Silverpeas calendar importation servlet. * * @return a URLConnection object to connect to the Silverpeas calendar importation servlet. * @throws MalformedURLException/*from w ww .j a va2 s .c o m*/ * @throws IOException */ private final URLConnection getServletConnection() throws MalformedURLException, IOException { URL urlServlet = new URL(servletURL); URLConnection con = urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/json"); return con; }
From source file:org.jenkinsci.plugins.mber.FileDownloadCallable.java
@Override public JSONObject invoke(final File file, final VirtualChannel channel) { InputStream istream = null;//from w ww .j ava 2 s.com OutputStream ostream = null; try { this.fileName = file.getName(); // Avoid I/O errors by setting attributes on the connection before getting data from it. final String redirectedURL = followRedirects(this.url); final URLConnection connection = new URL(redirectedURL).openConnection(); connection.setUseCaches(false); // Track expected bytes vs. downloaded bytes so we can retry corrupt downloads. final long expectedByteCount = connection.getContentLength(); istream = connection.getInputStream(); ostream = new LoggingOutputStream(new FilePath(file).write(), this, expectedByteCount); final long downloadedByteCount = IOUtils.copyLarge(istream, ostream); if (downloadedByteCount < expectedByteCount) { final long missingByteCount = expectedByteCount - downloadedByteCount; return MberJSON.failed(String.format("Missing %d bytes in %s", missingByteCount, this.fileName)); } return MberJSON.success(); } catch (final LoggingInterruptedException e) { return MberJSON.aborted(e); } catch (final Exception e) { return MberJSON.failed(e); } finally { // Close the input and output streams so other build steps can access those files. IOUtils.closeQuietly(istream); IOUtils.closeQuietly(ostream); } }
From source file:XMLResourceBundleControl.java
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { if ((baseName == null) || (locale == null) || (format == null) || (loader == null)) { throw new NullPointerException(); }/*from ww w . j av a 2s.c om*/ ResourceBundle bundle = null; if (!format.equals(XML)) { return null; } String bundleName = toBundleName(baseName, locale); String resourceName = toResourceName(bundleName, format); URL url = loader.getResource(resourceName); if (url == null) { return null; } URLConnection connection = url.openConnection(); if (connection == null) { return null; } if (reload) { connection.setUseCaches(false); } InputStream stream = connection.getInputStream(); if (stream == null) { return null; } BufferedInputStream bis = new BufferedInputStream(stream); bundle = new XMLResourceBundle(bis); bis.close(); return bundle; }
From source file:com.example.m.niceproject.service.GoogleMapsGeocodingService.java
public void refreshLocation(Location location) { new AsyncTask<Location, Void, LocationResult>() { @Override//from w w w. j a va2s. c o m protected LocationResult doInBackground(Location... locations) { Location location = locations[0]; String endpoint = String.format( "https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&key=%s", location.getLatitude(), location.getLongitude(), API_KEY); try { URL url = new URL(endpoint); URLConnection connection = url.openConnection(); connection.setUseCaches(false); InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } JSONObject data = new JSONObject(result.toString()); JSONArray results = data.optJSONArray("results"); if (results.length() == 0) { error = new ReverseGeolocationException("Could not reverse geocode " + location.getLatitude() + ", " + location.getLongitude()); return null; } LocationResult locationResult = new LocationResult(); locationResult.populate(results.optJSONObject(0)); return locationResult; } catch (Exception e) { error = e; } return null; } @Override protected void onPostExecute(LocationResult location) { if (location == null && error != null) { listener.geocodeFailure(error); } else { listener.geocodeSuccess(location); } } }.execute(location); }
From source file:org.digidoc4j.impl.bdoc.SKTimestampDataLoader.java
@Override public byte[] post(String url, byte[] content) { logger.info("Getting timestamp from " + url); OutputStream out = null;//from w w w.java 2 s. c om InputStream inputStream = null; byte[] result = null; try { URLConnection connection = new URL(url).openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestProperty("Content-Type", "application/timestamp-query"); connection.setRequestProperty("Content-Transfer-Encoding", "binary"); connection.setRequestProperty("User-Agent", userAgent); out = connection.getOutputStream(); IOUtils.write(content, out); inputStream = connection.getInputStream(); result = IOUtils.toByteArray(inputStream); } catch (IOException e) { throw new DSSException("An error occured while HTTP POST for url '" + url + "' : " + e.getMessage(), e); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(inputStream); } return result; }