Archive for 23. July 2006
Setting the HTTP User-agent in Java
Just testing the newly installed iG:Syntax Hiliter Plugin while trying not to post something completely useless ;)
JAVA5:
-
try {
-
connection = url.openConnection();
-
// here's the essential line:
-
connection.addRequestProperty("User-Agent", userAgent);
-
is = connection.getInputStream();
-
// ...do something with that input stream...
-
LOG.error("Error reading " + url, e);
-
} finally {
-
if (is != null) {
-
try {
-
is.close();
-
if (LOG.isWarnEnabled()) {
-
LOG.warn("Error closing input stream: " + url, e);
-
}
-
} // catch
-
} // if
-
} // finally