当前位置: 首页>>代码示例>>Java>>正文


Java URLConnection.setIfModifiedSince方法代码示例

本文整理汇总了Java中java.net.URLConnection.setIfModifiedSince方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.setIfModifiedSince方法的具体用法?Java URLConnection.setIfModifiedSince怎么用?Java URLConnection.setIfModifiedSince使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLConnection的用法示例。


在下文中一共展示了URLConnection.setIfModifiedSince方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setIfModifiedSince

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Confirm that {@link URLConnection#setIfModifiedSince} causes an If-Modified-Since header with a
 * GMT timestamp.
 *
 * https://code.google.com/p/android/issues/detail?id=66135
 */
@Test public void setIfModifiedSince() throws Exception {
  server.enqueue(new MockResponse().setBody("A"));

  URL url = server.url("/").url();
  URLConnection connection = urlFactory.open(url);
  connection.setIfModifiedSince(1393666200000L);
  assertEquals("A", readAscii(connection));
  RecordedRequest request = server.takeRequest();
  String ifModifiedSinceHeader = request.getHeader("If-Modified-Since");
  assertEquals("Sat, 01 Mar 2014 09:30:00 GMT", ifModifiedSinceHeader);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:18,代码来源:UrlConnectionCacheTest.java

示例2: applyConditionals

import java.net.URLConnection; //导入方法依赖的package包/类
void applyConditionals(URLConnection urlConnection) {
    if(lastModified != 0L) {
        urlConnection.setIfModifiedSince(lastModified);
    }
    if(entityTags != null && entityTags.length() > 0) {
        urlConnection.addRequestProperty("If-None-Match", entityTags);
    }
}
 
开发者ID:MikaGuraN,项目名称:HL4A,代码行数:9,代码来源:UrlModuleSourceProvider.java

示例3: openConnection

import java.net.URLConnection; //导入方法依赖的package包/类
private URLConnection openConnection(URL aSource) throws IOException {

            // set up the URL connection
            URLConnection connection = aSource.openConnection();
            // modify the headers
            // NB: things like user authentication could go in here too.
            if (hasTimestamp) {
                connection.setIfModifiedSince(timestamp);
            }

            // in case the plugin manager is its own project, this can become an authenticator
            boolean isSecureProcotol = "https".equalsIgnoreCase(aSource.getProtocol());
            boolean isAuthInfoSet = !Strings.isNullOrEmpty(aSource.getUserInfo());
            if (isAuthInfoSet) {
                if (!isSecureProcotol) {
                    throw new IOException("Basic auth is only supported for HTTPS!");
                }
                String basicAuth = Base64.encodeBytes(aSource.getUserInfo().getBytes(StandardCharsets.UTF_8));
                connection.setRequestProperty("Authorization", "Basic " + basicAuth);
            }

            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
                connection.setUseCaches(true);
                connection.setConnectTimeout(5000);
            }
            connection.setRequestProperty("ES-Version", Version.CURRENT.toString());
            connection.setRequestProperty("ES-Build-Hash", Build.CURRENT.hashShort());
            connection.setRequestProperty("User-Agent", "elasticsearch-plugin-manager");

            // connect to the remote site (may take some time)
            connection.connect();

            // First check on a 301 / 302 (moved) response (HTTP only)
            if (connection instanceof HttpURLConnection) {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                int responseCode = httpConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
                        responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
                        responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
                    String newLocation = httpConnection.getHeaderField("Location");
                    URL newURL = new URL(newLocation);
                    if (!redirectionAllowed(aSource, newURL)) {
                        return null;
                    }
                    return openConnection(newURL);
                }
                // next test for a 304 result (HTTP only)
                long lastModified = httpConnection.getLastModified();
                if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED
                        || (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
                    // not modified so no file download. just return
                    // instead and trace out something so the user
                    // doesn't think that the download happened when it
                    // didn't
                    return null;
                }
                // test for 401 result (HTTP only)
                if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    String message = "HTTP Authorization failure";
                    throw new IOException(message);
                }
            }

            //REVISIT: at this point even non HTTP connections may
            //support the if-modified-since behaviour -we just check
            //the date of the content and skip the write if it is not
            //newer. Some protocols (FTP) don't include dates, of
            //course.
            return connection;
        }
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:72,代码来源:HttpDownloadHelper.java

示例4: if

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
 * match the specified selector. If no <code>X509Certificate</code>s
 * match the selector, an empty <code>Collection</code> will be returned.
 *
 * @param selector a <code>CertSelector</code> used to select which
 *  <code>X509Certificate</code>s should be returned. Specify
 *  <code>null</code> to return all <code>X509Certificate</code>s.
 * @return a <code>Collection</code> of <code>X509Certificate</code>s that
 *         match the specified selector
 * @throws CertStoreException if an exception occurs
 */
@Override
@SuppressWarnings("unchecked")
public synchronized Collection<X509Certificate> engineGetCertificates
    (CertSelector selector) throws CertStoreException {

    if (ldap) {
        // caching mechanism, see the class description for more info.
        return (Collection<X509Certificate>)
            ldapCertStore.getCertificates(selector);
    }

    // Return the Certificates for this entry. It returns the cached value
    // if it is still current and fetches the Certificates otherwise.
    // For the caching details, see the top of this class.
    long time = System.currentTimeMillis();
    if (time - lastChecked < CHECK_INTERVAL) {
        if (debug != null) {
            debug.println("Returning certificates from cache");
        }
        return getMatchingCerts(certs, selector);
    }
    lastChecked = time;
    try {
        URLConnection connection = uri.toURL().openConnection();
        if (lastModified != 0) {
            connection.setIfModifiedSince(lastModified);
        }
        long oldLastModified = lastModified;
        try (InputStream in = connection.getInputStream()) {
            lastModified = connection.getLastModified();
            if (oldLastModified != 0) {
                if (oldLastModified == lastModified) {
                    if (debug != null) {
                        debug.println("Not modified, using cached copy");
                    }
                    return getMatchingCerts(certs, selector);
                } else if (connection instanceof HttpURLConnection) {
                    // some proxy servers omit last modified
                    HttpURLConnection hconn = (HttpURLConnection)connection;
                    if (hconn.getResponseCode()
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        if (debug != null) {
                            debug.println("Not modified, using cached copy");
                        }
                        return getMatchingCerts(certs, selector);
                    }
                }
            }
            if (debug != null) {
                debug.println("Downloading new certificates...");
            }
            // Safe cast since factory is an X.509 certificate factory
            certs = (Collection<X509Certificate>)
                factory.generateCertificates(in);
        }
        return getMatchingCerts(certs, selector);
    } catch (IOException | CertificateException e) {
        if (debug != null) {
            debug.println("Exception fetching certificates:");
            e.printStackTrace();
        }
    }
    // exception, forget previous values
    lastModified = 0;
    certs = Collections.emptySet();
    return certs;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:80,代码来源:URICertStore.java


注:本文中的java.net.URLConnection.setIfModifiedSince方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。