本文整理汇总了Java中java.net.HttpURLConnection.getLastModified方法的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.getLastModified方法的具体用法?Java HttpURLConnection.getLastModified怎么用?Java HttpURLConnection.getLastModified使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.getLastModified方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findTidsstempelForSenesteAPK
import java.net.HttpURLConnection; //导入方法依赖的package包/类
public static Long findTidsstempelForSenesteAPK() throws Exception {
if (APK_URL==null || APK_URL.length()==0) return null;
/*
final PackageManager pm = getPackageManager();
String apkName = "example.apk";
String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);
Toast.makeText(this, "VersionCode : " + info.versionCode + ", VersionName : " + info.versionName , Toast.LENGTH_LONG).show();
*/
URL url = new URL(APK_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode()!= HttpURLConnection.HTTP_OK) return null;
if (!url.getHost().equals(urlConnection.getURL().getHost())) return null; // ingen omdirigeringer
long lm = urlConnection.getLastModified();
return lm;
}
示例2: openConnection
import java.net.HttpURLConnection; //导入方法依赖的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;
}
示例3: download
import java.net.HttpURLConnection; //导入方法依赖的package包/类
/**
* Check if there is a new version of the script engine
*
* @throws IOException
* if any error occur
*/
private void download() throws IOException {
boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
String fileName;
final String os = System.getProperty( "os.name", "" ).toLowerCase();
if( os.contains( "windows" ) ) {
fileName = is32 ? "win32" : "win64";
} else if( os.contains( "mac" ) ) {
fileName = is32 ? "mac" : "mac64";
} else if( os.contains( "linux" ) ) {
fileName = is32 ? "linux-i686" : "linux-x86_64";
} else {
throw new IllegalStateException( "Unknown OS: " + os );
}
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName
+ ".zip" );
System.out.println( "\tDownload: " + url );
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if( target.exists() ) {
System.out.println( "\tUP-TP-DATE" );
conn.setIfModifiedSince( target.lastModified() );
}
InputStream input = conn.getInputStream();
command = target.getAbsolutePath() + "/js";
if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
return;
}
ZipInputStream zip = new ZipInputStream( input );
long lastModfied = conn.getLastModified();
do {
ZipEntry entry = zip.getNextEntry();
if( entry == null ) {
break;
}
if( entry.isDirectory() ) {
continue;
}
File file = new File( target, entry.getName() );
file.getParentFile().mkdirs();
Files.copy( zip, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
file.setLastModified( entry.getTime() );
if( "js".equals( file.getName() ) ) {
file.setExecutable( true );
}
} while( true );
target.setLastModified( lastModfied );
}