java


I'm trying to use Java's HttpURLConnection to do a “conditional get”, but I never get a 304 status code


Here is my code:
final HttpURLConnection conn = (HttpURLConnection) sourceURL.openConnection();
if (cachedPage != null) {
if (cachedPage.eTag != null) {
conn.setRequestProperty("If-None-Match", cachedPage.eTag);
}
conn.setIfModifiedSince(cachedPage.pageLastModified);
}
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
newCachedPage.eTag = conn.getHeaderField("ETag");
newCachedPage.pageLastModified = conn.getHeaderFieldDate("Last-Modified", 0);
} else if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Never reaches here
}
I never seem to get the HTTP_NOT_MODIFIED response code, even hitting the same server several times in quick succession - where there is definitely no change to the page. Also, conn.getHeaderField("ETag") always seems to respond null, and sometimes conn.getHeaderFieldDate("Last-Modified", 0) returns 0. I've tried this against a variety of web servers.
Can anyone tell me what I'm doing wrong?
You're all dependent on the server config.
If you get an Expires response header, then it just means that you don't need to request anything until the specified expire time. If you get a Last-Modified response header, then it means that you should be able to use If-Modified-Since to test it. If you get an ETag response header, then it means that you should be able to use If-None-Match to test it.
Lets take http://cdn3.sstatic.net/stackoverflow/img/favicon.ico as an example (the Stackoverflow's favicon image):
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
System.out.println(connection.getHeaderFields());
This gives:
{null=[HTTP/1.1 200 OK], ETag=["9d9bd8b1165cb1:0"], Date=[Wed, 17 Aug 2011 17:57:07 GMT], Content-Length=[1150], Last-Modified=[Wed, 06 Oct 2010 02:53:46 GMT], Content-Type=[image/x-icon], Connection=[keep-alive], Accept-Ranges=[bytes], Server=[nginx/0.8.36], X-Cache=[HIT], Cache-Control=[max-age=604800]}
Now, do a If-Modified-Since with the same value as Last-Modified:
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-Modified-Since", "Wed, 06 Oct 2010 02:53:46 GMT");
System.out.println(connection.getHeaderFields());
This gives as expected a 304:
{null=[HTTP/1.1 304 Not Modified], ETag=["9d9bd8b1165cb1:0"], Date=[Wed, 17 Aug 2011 17:57:42 GMT], Last-Modified=[Wed, 06 Oct 2010 02:53:46 GMT], Connection=[keep-alive], Server=[nginx/0.8.36], X-Cache=[HIT], Cache-Control=[max-age=604800]}
Now, do a If-None-Match with the same value as ETag:
URLConnection connection = new URL("http://cdn3.sstatic.net/stackoverflow/img/favicon.ico").openConnection();
connection.setRequestProperty("If-None-Match", "9d9bd8b1165cb1:0");
System.out.println(connection.getHeaderFields());
This gives unexpectedly a 200:
{null=[HTTP/1.1 200 OK], ETag=["9d9bd8b1165cb1:0"], Date=[Wed, 17 Aug 2011 18:01:42 GMT], Content-Length=[1150], Last-Modified=[Wed, 06 Oct 2010 02:53:46 GMT], Content-Type=[image/x-icon], Connection=[keep-alive], Accept-Ranges=[bytes], Server=[nginx/0.8.36], X-Cache=[HIT], Cache-Control=[max-age=604800]}
Even more surprising, when the both headers are set with random garbage value as ETag, the server still gives a 304. This is an indication that the If-None-Match is completely ignored by the server behind http://cdn3.sstatic.net. That might be a (proxy) configuration issue or be done fully awarely (not for obvious reasons imho).
Or shorter :)
Just try set connection timeout other than 0.
conn.setConnectionTimeout( 3000);
Do it after .openConnection()
final HttpURLConnection conn = (HttpURLConnection) sourceURL.openConnection();
conn.setConnectionTimeout( 3000);
If not set also readTimeout other than 0.
conn.setReadTimeout( 3000);

Related Links

Auto-locating a client's server
Making cut/replace methods with JTextArea
Which HTML tag is the proper one to use for Java applets (APPLET, EMBED, OBJECT)?
Embeddable Java HTTP Servers
Is there a Swing hack to query or set Z-order of a window?
How do I read a resource file from a Java jar file?
Hibernate criteria: Joining table without a mapped association
missing resource exception
Apache beehive and localizing default pager for DataGrid
Datatype of SUM result in MySQL
QCompleter and QLineEdit for multiple words
Draw offscreen with JOGL
Regex in java question, multiple matches
Throwing exceptions to control flow - code smell?
In Java, can Class.forName ever return null?
XML Diff and Merge

Categories

HOME
ms-access
comparison
layout
spring-cloud-stream
frameworks
packages
v8
dxl
survey
umd
sql-server-2016
constraint-programming
slurm
session-variables
fortumo
ghc
highlight.js
invantive-sql
apache-metamodel
firefox-webextensions
h2db
phpfox
publish
saas
subdomains
xquery-3.0
form-data
file-format
x11-forwarding
hot-module-replacement
objectlistview
code-search-engine
ssjs
gulp-sourcemaps
turbogears
acoustics
skeleton-css-boilerplate
logfiles
long-polling
smartcontracts
termination
jquery-nestable
typescript1.8
sage-one
knockout-components
magma
np-complete
python-cryptography
spring-mongodb
blacklist
topbeat
libpng
fody
thrust
bstr
connect-by
captivenetwork
system.management
wso2cloud
prerequisites
thredds
security-testing
kcachegrind
emailrelay
ami
dereference
apache-commons-net
geonetwork
gadt
internet-connection
bitcoinj
mesa
tld
aapt
p4java
hyprlinkr
runtime.exec
seed
sqlperformance
sharp-repository
eventlistener
chuck
libstdc++
quickdialog
rdoc
coderush
multipage
whoosh
mercurial-server
window-management
mod-auth
coredump
servicehost
web-application-design
vc90
compiler-specific
ctp4
w3wp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App