java.net.URLConnection.setUseCaches()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(651)

本文整理了Java中java.net.URLConnection.setUseCaches()方法的一些代码示例,展示了URLConnection.setUseCaches()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.setUseCaches()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:setUseCaches

URLConnection.setUseCaches介绍

[英]Sets the flag indicating whether this connection allows to use caches or not. This method can only be called prior to the connection establishment.
[中]设置指示此连接是否允许使用缓存的标志。只能在建立连接之前调用此方法。

代码示例

代码示例来源:origin: stackoverflow.com

  1. HttpURLConnection httpUrlConnection = null;
  2. URL url = new URL("http://example.com/server.cgi");
  3. httpUrlConnection = (HttpURLConnection) url.openConnection();
  4. httpUrlConnection.setUseCaches(false);
  5. httpUrlConnection.setDoOutput(true);
  6. httpUrlConnection.setRequestMethod("POST");
  7. httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
  8. httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
  9. httpUrlConnection.setRequestProperty(
  10. "Content-Type", "multipart/form-data;boundary=" + this.boundary);

代码示例来源:origin: log4j/log4j

  1. public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
  2. URLConnection uConn = url.openConnection();
  3. uConn.setUseCaches(false);
  4. InputStream stream = uConn.getInputStream();
  5. try {
  6. InputSource src = new InputSource(stream);
  7. src.setSystemId(url.toString());
  8. return parser.parse(src);
  9. } finally {
  10. stream.close();
  11. }
  12. }
  13. public String toString() {

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL(strUrl);
  2. URLConnection connection = url.openConnection();
  3. connection.setUseCaches(true);
  4. Object response = connection.getContent();
  5. if (response instanceof Bitmap) {
  6. Bitmap bitmap = (Bitmap)response;
  7. }

代码示例来源:origin: org.codehaus.groovy/groovy

  1. final URLConnection connection = url.openConnection();
  2. if (parameters != null) {
  3. if (parameters.containsKey("connectTimeout")) {
  4. connection.setUseCaches(DefaultGroovyMethods.asType(parameters.get("useCaches"), Boolean.class));
  5. Map<String, CharSequence> properties = (Map<String, CharSequence>) parameters.get("requestProperties");
  6. for (Map.Entry<String, CharSequence> entry : properties.entrySet()) {
  7. connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
  8. return connection.getInputStream();

代码示例来源:origin: org.freemarker/freemarker

  1. /**
  2. * @param useCaches {@code null} if this aspect wasn't set in the parent {@link TemplateLoader}.
  3. */
  4. URLTemplateSource(URL url, Boolean useCaches) throws IOException {
  5. this.url = url;
  6. this.conn = url.openConnection();
  7. this.useCaches = useCaches;
  8. if (useCaches != null) {
  9. conn.setUseCaches(useCaches.booleanValue());
  10. }
  11. }

代码示例来源:origin: javamelody/javamelody

  1. /**
  2. * Ouvre la connection http.
  3. * @return Object
  4. * @throws IOException Exception de communication
  5. */
  6. private URLConnection openConnection() throws IOException {
  7. final URLConnection connection = url.openConnection();
  8. connection.setUseCaches(false);
  9. if (CONNECTION_TIMEOUT > 0) {
  10. connection.setConnectTimeout(CONNECTION_TIMEOUT);
  11. }
  12. if (READ_TIMEOUT > 0) {
  13. connection.setReadTimeout(READ_TIMEOUT);
  14. }
  15. // grâce à cette propriété, l'application retournera un flux compressé si la taille
  16. // dépasse x Ko
  17. connection.setRequestProperty("Accept-Encoding", "gzip");
  18. if (headers != null) {
  19. for (final Map.Entry<String, String> entry : headers.entrySet()) {
  20. connection.setRequestProperty(entry.getKey(), entry.getValue());
  21. }
  22. }
  23. if (url.getUserInfo() != null) {
  24. final String authorization = Base64Coder.encodeString(url.getUserInfo());
  25. connection.setRequestProperty("Authorization", "Basic " + authorization);
  26. }
  27. return connection;
  28. }

代码示例来源:origin: cSploit/android

  1. protected void writeCall(String methodName, Object[] args) throws IOException {
  2. huc = u.openConnection();
  3. huc.setDoOutput(true);
  4. huc.setDoInput(true);
  5. huc.setUseCaches(false);
  6. huc.setRequestProperty("Content-Type", "binary/message-pack");
  7. huc.setReadTimeout(0);
  8. OutputStream os = huc.getOutputStream();
  9. Packer pk = msgpack.createPacker(os);
  10. pk.writeArrayBegin(args.length+1);
  11. pk.write(methodName);
  12. for (Object arg : args)
  13. pk.write(arg);
  14. pk.writeArrayEnd();
  15. pk.close();
  16. os.close();
  17. }

代码示例来源:origin: stackoverflow.com

  1. System.setProperty("http.keepAlive", "false");
  2. HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
  3. conn.setUseCaches(false);
  4. conn.setRequestProperty("User-Agent", useragent);
  5. conn.setConnectTimeout(30000);
  6. conn.setDoOutput(true);
  7. conn.setDoInput(true);
  8. consumer.sign(conn);
  9. conn.connect();
  10. InputSource is = new InputSource(conn.getInputStream());

代码示例来源:origin: stackoverflow.com

  1. String urlParameters = "param1=a&param2=b&param3=c";
  2. byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
  3. int postDataLength = postData.length;
  4. String request = "http://example.com/index.php";
  5. URL url = new URL( request );
  6. HttpURLConnection conn= (HttpURLConnection) url.openConnection();
  7. conn.setDoOutput( true );
  8. conn.setInstanceFollowRedirects( false );
  9. conn.setRequestMethod( "POST" );
  10. conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
  11. conn.setRequestProperty( "charset", "utf-8");
  12. conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
  13. conn.setUseCaches( false );
  14. try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
  15. wr.write( postData );
  16. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Opens a resource of the specified name for reading. Controls caching,
  3. * that is important when the same jar is reloaded using custom classloader.
  4. */
  5. public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass, boolean useCache) throws IOException {
  6. URL url = getResourceUrl(resourceName, callingClass);
  7. if (url != null) {
  8. URLConnection urlConnection = url.openConnection();
  9. urlConnection.setUseCaches(useCache);
  10. return urlConnection.getInputStream();
  11. }
  12. return null;
  13. }

代码示例来源:origin: stackoverflow.com

  1. String addr = "http://172.26.41.18:8080/domain/list";
  2. URL url = new URL(addr);
  3. HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
  4. httpCon.setUseCaches(false);
  5. httpCon.setAllowUserInteraction(false);
  6. httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
  7. System.out.println(httpCon.getResponseCode());
  8. System.out.println(httpCon.getResponseMessage());

代码示例来源:origin: ronmamo/reflections

  1. public Dir createDir(URL url) throws Exception {
  2. try {
  3. URLConnection urlConnection = url.openConnection();
  4. if (urlConnection instanceof JarURLConnection) {
  5. urlConnection.setUseCaches(false);
  6. return new ZipDir(((JarURLConnection) urlConnection).getJarFile());
  7. }
  8. } catch (Throwable e) { /*fallback*/ }
  9. java.io.File file = getFile(url);
  10. if (file != null) {
  11. return new ZipDir(new JarFile(file));
  12. }
  13. return null;
  14. }
  15. },

代码示例来源:origin: openmrs/openmrs-core

  1. /**
  2. * Downloads the contents of a URL and copies them to a string (Borrowed from oreilly)
  3. *
  4. * @param url
  5. * @return InputStream of contents
  6. * @should return a valid input stream for old module urls
  7. */
  8. public static InputStream getURLStream(URL url) {
  9. InputStream in = null;
  10. try {
  11. URLConnection uc = url.openConnection();
  12. uc.setDefaultUseCaches(false);
  13. uc.setUseCaches(false);
  14. uc.setRequestProperty("Cache-Control", "max-age=0,no-cache");
  15. uc.setRequestProperty("Pragma", "no-cache");
  16. log.debug("Logging an attempt to connect to: " + url);
  17. in = openConnectionCheckRedirects(uc);
  18. }
  19. catch (IOException io) {
  20. log.warn("io while reading: " + url, io);
  21. }
  22. return in;
  23. }

代码示例来源:origin: stackoverflow.com

  1. URL myURL = new URL(serviceURL);
  2. HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
  3. String userCredentials = "username:password";
  4. String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
  5. myURLConnection.setRequestProperty ("Authorization", basicAuth);
  6. myURLConnection.setRequestMethod("POST");
  7. myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  8. myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
  9. myURLConnection.setRequestProperty("Content-Language", "en-US");
  10. myURLConnection.setUseCaches(false);
  11. myURLConnection.setDoInput(true);
  12. myURLConnection.setDoOutput(true);

代码示例来源:origin: org.codehaus.groovy/groovy

  1. /**
  2. * Opens an {@link InputStream} reading from the given URL without
  3. * caching the stream. This prevents file descriptor leaks when reading
  4. * from file system URLs.
  5. *
  6. * @param url the URL to connect to
  7. * @return an input stream reading from the URL connection
  8. */
  9. public static InputStream openUncachedStream(URL url) throws IOException {
  10. URLConnection urlConnection = url.openConnection();
  11. urlConnection.setUseCaches(false);
  12. return urlConnection.getInputStream();
  13. }
  14. }

代码示例来源:origin: spotbugs/spotbugs

  1. /**
  2. * When URL Connection uses cache, it may keep file handler. This method open connection without caching to avoid
  3. * file handler leak.
  4. *
  5. * @return opened {@link URLConnection} which does not use cache to load data
  6. * @see <a href="https://github.com/spotbugs/spotbugs/issues/589">related GitHub issue</a>
  7. */
  8. @CheckReturnValue
  9. @NonNull
  10. public static URLConnection openNonCachedConnection(@NonNull URL u) throws IOException {
  11. URLConnection uc = u.openConnection();
  12. if (uc instanceof JarURLConnection) {
  13. uc.setUseCaches(false);
  14. }
  15. return uc;
  16. }

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL(targetURL);
  2. connection = (HttpURLConnection) url.openConnection();
  3. connection.setRequestMethod("POST");
  4. connection.setRequestProperty("Content-Type",
  5. "application/x-www-form-urlencoded");
  6. connection.setRequestProperty("Content-Length",
  7. Integer.toString(urlParameters.getBytes().length));
  8. connection.setRequestProperty("Content-Language", "en-US");
  9. connection.setUseCaches(false);
  10. connection.setDoOutput(true);
  11. connection.getOutputStream());
  12. wr.writeBytes(urlParameters);
  13. wr.close();
  14. InputStream is = connection.getInputStream();
  15. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  16. StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+

代码示例来源:origin: apache/rocketmq

  1. public static String file2String(final URL url) {
  2. InputStream in = null;
  3. try {
  4. URLConnection urlConnection = url.openConnection();
  5. urlConnection.setUseCaches(false);
  6. in = urlConnection.getInputStream();
  7. int len = in.available();
  8. byte[] data = new byte[len];
  9. in.read(data, 0, len);
  10. return new String(data, "UTF-8");
  11. } catch (Exception ignored) {
  12. } finally {
  13. if (null != in) {
  14. try {
  15. in.close();
  16. } catch (IOException ignored) {
  17. }
  18. }
  19. }
  20. return null;
  21. }

代码示例来源:origin: stackoverflow.com

  1. URL url;
  2. URLConnection urlConn;
  3. DataOutputStream printout;
  4. DataInputStream input;
  5. url = new URL (getCodeBase().toString() + "env.tcgi");
  6. urlConn = url.openConnection();
  7. urlConn.setDoInput (true);
  8. urlConn.setDoOutput (true);
  9. urlConn.setUseCaches (false);
  10. urlConn.setRequestProperty("Content-Type","application/json");
  11. urlConn.setRequestProperty("Host", "android.schoolportal.gr");
  12. urlConn.connect();
  13. //Create JSONObject here
  14. JSONObject jsonParam = new JSONObject();
  15. jsonParam.put("ID", "25");
  16. jsonParam.put("description", "Real");
  17. jsonParam.put("enable", "true");

代码示例来源:origin: micronaut-projects/micronaut-core

  1. @Nonnull
  2. @Override
  3. public InputStream asInputStream() throws IOException {
  4. URLConnection con = this.url.openConnection();
  5. con.setUseCaches(true);
  6. try {
  7. return con.getInputStream();
  8. } catch (IOException ex) {
  9. if (con instanceof HttpURLConnection) {
  10. ((HttpURLConnection) con).disconnect();
  11. }
  12. throw ex;
  13. }
  14. }

相关文章

URLConnection类方法