Difference between revisions of "JGridstart/Java user agents"

From PDP/Grid Wiki
Jump to navigationJump to search
m
(add user agent note)
Line 37: Line 37:
 
| <tt>Java/1.6.0_0</tt>
 
| <tt>Java/1.6.0_0</tt>
 
|}
 
|}
 +
 +
== Setting the user-agent from Java ==
 +
jGridstart sets the user-agent so that it can be recognised by the web server, including its version number. This is done by setting the system property <tt>[http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html#ncnt http.agent]</tt> (Java still appends something like <tt>Java/1.6.0_10</tt> to it).
 +
 +
There is a little problem with java web start. When the system property <tt>http.agent</tt> is set from within a java web start application, it is not actually used when a http connection is made. This can be remedied by either setting the property from the <tt>JNLP</tt> file, or by setting the request property <tt>User-Agent</tt> on the <tt>URLConnection</tt>. jGridstart uses this as a workaround:
 +
<code>
 +
  static URLConnection URLopenConnection(URL url) throws IOException {
 +
    URLConnection conn = url.openConnection();
 +
    if (conn instanceof HttpURLConnection) {
 +
      String agent = System.getProperty("http.agent") +
 +
                      " Java/"+System.getProperty("java.version");
 +
      if (agent != null)
 +
        ((HttpURLConnection)conn).setRequestProperty("User-Agent", agent);
 +
    }
 +
    return conn;
 +
  }
 +
</code>
 +
See also [http://www.noizeramp.com/article.php?article=se-networking_specifics_under_Java_Web_StartNetworking specifics under Java Web Start].

Revision as of 10:20, 14 April 2010

It is possible to detect on the server-side something about the user's Java environment by inspecting the request headers that Java sends. There are differences when a connection is made from the Java program using URLConnection, or when a Java Web Start application is being downloaded.

Java Web Start

Vendor HTTP Header
User-Agent UA-Java-Version
Sun JNLP/1.5 javaws/1.5.0_19 (b02) J2SE/1.5.0_19 1.5.0_19
Sun JNLP/6.0 javaws/1.6.0_17 (b04) Java/1.6.0_17 1.6.0_17
OpenJDK Java/1.6.0_0 (not supplied)

URLConnection

Vendor HTTP header User-Agent
Sun Java/1.5.0_19
Sun Java/1.6.0_17
OpenJDK Java/1.6.0_0

Setting the user-agent from Java

jGridstart sets the user-agent so that it can be recognised by the web server, including its version number. This is done by setting the system property http.agent (Java still appends something like Java/1.6.0_10 to it).

There is a little problem with java web start. When the system property http.agent is set from within a java web start application, it is not actually used when a http connection is made. This can be remedied by either setting the property from the JNLP file, or by setting the request property User-Agent on the URLConnection. jGridstart uses this as a workaround:

 static URLConnection URLopenConnection(URL url) throws IOException {
   URLConnection conn = url.openConnection();
   if (conn instanceof HttpURLConnection) {
     String agent = System.getProperty("http.agent") +
                      " Java/"+System.getProperty("java.version");
     if (agent != null)
       ((HttpURLConnection)conn).setRequestProperty("User-Agent", agent);
   }
   return conn;
 }

See also specifics under Java Web Start.