我正在尝试在我的应用程序中模拟一个类似于“缓存验证”的过程。我将在设备上下载我的new应用程序的新版本(基于android),但我只想下载文件的新版本,基于etag比较。有没有人举例说明如何在Android中使用Etag机制?
发布于 2013-01-10 05:10:00
您可以从HttpURLConnection对象访问ETag字段,如下所示:
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
String etag = conn.getHeaderField("ETag");当然,您需要确保在其上进行测试的服务器支持ETags。
发布于 2012-12-14 22:39:41
也许这个库(kevinsawicki)中的类"HttpRequest“会对你有所帮助。
例如:
File latest = new File("/data/cache.json");
HttpRequest request = HttpRequest.get("http://google.com");
//Copy response to file
request.body(latest);
//Store eTag of response
String eTag = request.eTag();
//Later you can check if changes exist
boolean unchanged = HttpRequest.get("http://google.com")
.ifNoneMatch(eTag)
.notModified();发布于 2013-10-24 01:27:30
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.d("Http Response:", response.getFirstHeader("etag").toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}https://stackoverflow.com/questions/7973609
复制相似问题