本文整理了Java中java.net.URLConnection.setUseCaches()
方法的一些代码示例,展示了URLConnection.setUseCaches()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.setUseCaches()
方法的具体详情如下:
包路径:java.net.URLConnection
类名称: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
HttpURLConnection httpUrlConnection = null;
URL url = new URL("http://example.com/server.cgi");
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + this.boundary);
代码示例来源:origin: log4j/log4j
public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
URLConnection uConn = url.openConnection();
uConn.setUseCaches(false);
InputStream stream = uConn.getInputStream();
try {
InputSource src = new InputSource(stream);
src.setSystemId(url.toString());
return parser.parse(src);
} finally {
stream.close();
}
}
public String toString() {
代码示例来源:origin: stackoverflow.com
URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
Bitmap bitmap = (Bitmap)response;
}
代码示例来源:origin: org.codehaus.groovy/groovy
final URLConnection connection = url.openConnection();
if (parameters != null) {
if (parameters.containsKey("connectTimeout")) {
connection.setUseCaches(DefaultGroovyMethods.asType(parameters.get("useCaches"), Boolean.class));
Map<String, CharSequence> properties = (Map<String, CharSequence>) parameters.get("requestProperties");
for (Map.Entry<String, CharSequence> entry : properties.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
return connection.getInputStream();
代码示例来源:origin: org.freemarker/freemarker
/**
* @param useCaches {@code null} if this aspect wasn't set in the parent {@link TemplateLoader}.
*/
URLTemplateSource(URL url, Boolean useCaches) throws IOException {
this.url = url;
this.conn = url.openConnection();
this.useCaches = useCaches;
if (useCaches != null) {
conn.setUseCaches(useCaches.booleanValue());
}
}
代码示例来源:origin: javamelody/javamelody
/**
* Ouvre la connection http.
* @return Object
* @throws IOException Exception de communication
*/
private URLConnection openConnection() throws IOException {
final URLConnection connection = url.openConnection();
connection.setUseCaches(false);
if (CONNECTION_TIMEOUT > 0) {
connection.setConnectTimeout(CONNECTION_TIMEOUT);
}
if (READ_TIMEOUT > 0) {
connection.setReadTimeout(READ_TIMEOUT);
}
// grâce à cette propriété, l'application retournera un flux compressé si la taille
// dépasse x Ko
connection.setRequestProperty("Accept-Encoding", "gzip");
if (headers != null) {
for (final Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (url.getUserInfo() != null) {
final String authorization = Base64Coder.encodeString(url.getUserInfo());
connection.setRequestProperty("Authorization", "Basic " + authorization);
}
return connection;
}
代码示例来源:origin: cSploit/android
protected void writeCall(String methodName, Object[] args) throws IOException {
huc = u.openConnection();
huc.setDoOutput(true);
huc.setDoInput(true);
huc.setUseCaches(false);
huc.setRequestProperty("Content-Type", "binary/message-pack");
huc.setReadTimeout(0);
OutputStream os = huc.getOutputStream();
Packer pk = msgpack.createPacker(os);
pk.writeArrayBegin(args.length+1);
pk.write(methodName);
for (Object arg : args)
pk.write(arg);
pk.writeArrayEnd();
pk.close();
os.close();
}
代码示例来源:origin: stackoverflow.com
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
consumer.sign(conn);
conn.connect();
InputSource is = new InputSource(conn.getInputStream());
代码示例来源:origin: stackoverflow.com
String urlParameters = "param1=a¶m2=b¶m3=c";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String request = "http://example.com/index.php";
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}
代码示例来源:origin: redisson/redisson
/**
* Opens a resource of the specified name for reading. Controls caching,
* that is important when the same jar is reloaded using custom classloader.
*/
public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass, boolean useCache) throws IOException {
URL url = getResourceUrl(resourceName, callingClass);
if (url != null) {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(useCache);
return urlConnection.getInputStream();
}
return null;
}
代码示例来源:origin: stackoverflow.com
String addr = "http://172.26.41.18:8080/domain/list";
URL url = new URL(addr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setUseCaches(false);
httpCon.setAllowUserInteraction(false);
httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463");
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
代码示例来源:origin: ronmamo/reflections
public Dir createDir(URL url) throws Exception {
try {
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
urlConnection.setUseCaches(false);
return new ZipDir(((JarURLConnection) urlConnection).getJarFile());
}
} catch (Throwable e) { /*fallback*/ }
java.io.File file = getFile(url);
if (file != null) {
return new ZipDir(new JarFile(file));
}
return null;
}
},
代码示例来源:origin: openmrs/openmrs-core
/**
* Downloads the contents of a URL and copies them to a string (Borrowed from oreilly)
*
* @param url
* @return InputStream of contents
* @should return a valid input stream for old module urls
*/
public static InputStream getURLStream(URL url) {
InputStream in = null;
try {
URLConnection uc = url.openConnection();
uc.setDefaultUseCaches(false);
uc.setUseCaches(false);
uc.setRequestProperty("Cache-Control", "max-age=0,no-cache");
uc.setRequestProperty("Pragma", "no-cache");
log.debug("Logging an attempt to connect to: " + url);
in = openConnectionCheckRedirects(uc);
}
catch (IOException io) {
log.warn("io while reading: " + url, io);
}
return in;
}
代码示例来源:origin: stackoverflow.com
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
代码示例来源:origin: org.codehaus.groovy/groovy
/**
* Opens an {@link InputStream} reading from the given URL without
* caching the stream. This prevents file descriptor leaks when reading
* from file system URLs.
*
* @param url the URL to connect to
* @return an input stream reading from the URL connection
*/
public static InputStream openUncachedStream(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
return urlConnection.getInputStream();
}
}
代码示例来源:origin: spotbugs/spotbugs
/**
* When URL Connection uses cache, it may keep file handler. This method open connection without caching to avoid
* file handler leak.
*
* @return opened {@link URLConnection} which does not use cache to load data
* @see <a href="https://github.com/spotbugs/spotbugs/issues/589">related GitHub issue</a>
*/
@CheckReturnValue
@NonNull
public static URLConnection openNonCachedConnection(@NonNull URL u) throws IOException {
URLConnection uc = u.openConnection();
if (uc instanceof JarURLConnection) {
uc.setUseCaches(false);
}
return uc;
}
代码示例来源:origin: stackoverflow.com
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
代码示例来源:origin: apache/rocketmq
public static String file2String(final URL url) {
InputStream in = null;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
in = urlConnection.getInputStream();
int len = in.available();
byte[] data = new byte[len];
in.read(data, 0, len);
return new String(data, "UTF-8");
} catch (Exception ignored) {
} finally {
if (null != in) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
return null;
}
代码示例来源:origin: stackoverflow.com
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
url = new URL (getCodeBase().toString() + "env.tcgi");
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.setRequestProperty("Host", "android.schoolportal.gr");
urlConn.connect();
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
jsonParam.put("ID", "25");
jsonParam.put("description", "Real");
jsonParam.put("enable", "true");
代码示例来源:origin: micronaut-projects/micronaut-core
@Nonnull
@Override
public InputStream asInputStream() throws IOException {
URLConnection con = this.url.openConnection();
con.setUseCaches(true);
try {
return con.getInputStream();
} catch (IOException ex) {
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
内容来源于网络,如有侵权,请联系作者删除!