return null;
}
try {
URL url = new URL(location);
URLConnection con = url.openConnection();
// do we have if-modified-since or etag headers to set?
if (condition != null && condition != Undefined.instance) {
if (condition instanceof Scriptable) {
Scriptable scr = (Scriptable) condition;
if ("Date".equals(scr.getClassName())) {
Date date = new Date((long) ScriptRuntime.toNumber(scr));
con.setIfModifiedSince(date.getTime());
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.UK);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
con.setRequestProperty("If-Modified-Since", format.format(date));
}else {
con.setRequestProperty("If-None-Match", scr.toString());
}
} else {
con.setRequestProperty("If-None-Match", condition.toString());
}
}
String httpUserAgent = app.getProperty("httpUserAgent");
if (httpUserAgent != null) {
con.setRequestProperty("User-Agent", httpUserAgent);
}
if (timeout != null && timeout != Undefined.instance) {
int time = ScriptRuntime.toInt32(timeout);
con.setConnectTimeout(time);
con.setReadTimeout(time);
}
con.setAllowUserInteraction(false);
String filename = url.getFile();
String contentType = con.getContentType();
long lastmod = con.getLastModified();
String etag = con.getHeaderField("ETag");
int length = con.getContentLength();
int resCode = 0;
if (con instanceof HttpURLConnection) {
resCode = ((HttpURLConnection) con).getResponseCode();
}
ByteArrayOutputStream body = new ByteArrayOutputStream();
if ((length != 0) && (resCode != 304)) {
InputStream in = new BufferedInputStream(con.getInputStream());
byte[] b = new byte[1024];
int read;
while ((read = in.read(b)) > -1)
body.write(b, 0, read);