We define in our Java application a custom HTTP User-Agent containing the following:
- Software version
- User language
- Platform information (operating system family + release name)
- Java version
We want this user agent to be applied to all HTTP connections created by the application, including those we open manually, but also those automatically created by the JRE, for example when a JEditorPane
resolves external images referenced inside HTML code.
For this, we set the "http.agent"
system property to points 1/2/3 (letting the JRE add by itself the Java version) at the startup of our application:
System.setProperty("http.agent", Version.getAgentString());
This works great when we run the application from a jar, but not from Java Web Start.
As a workaround, we manually set our full User-Agent to the connections we create manually:
public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {
HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
connection.setRequestProperty("User-Agent", Version.getFullAgentString());
return connection;
}
But this does not handle the case where the connection is created by the JRE (JEditorPane example).
How can we set the user agent in this case ?
We have tried to change the value of sun.net.www.protocol.http.HttpURLConnection.userAgent
by using reflection with this example, but it doesn't work, we're facing an IllegalAccessException
.
We cannot neither set the User-Agent in the JNLP file as it is not possible to determine client information (user language + platform).