Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set default HTTP User Agent from a Java Web Start application?
    primarykey
    data
    text
    <p>We define in our Java application a custom HTTP User-Agent containing the following:</p> <ol> <li>Software version</li> <li>User language</li> <li>Platform information (operating system family + release name)</li> <li>Java version</li> </ol> <p>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 <code>JEditorPane</code> resolves external images referenced inside HTML code.</p> <p>For this, we set the <code>"http.agent"</code> system property to points 1/2/3 (letting the <a href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#MiscHTTP" rel="nofollow noreferrer">JRE add by itself the Java version</a>) at the startup of our application:</p> <pre><code>System.setProperty("http.agent", Version.getAgentString()); </code></pre> <p>This works great when we run the application from a jar, but not from Java Web Start.</p> <p>As a workaround, we manually set our full User-Agent to the connections we create manually:</p> <pre><code>public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException { HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection(); connection.setRequestProperty("User-Agent", Version.getFullAgentString()); return connection; } </code></pre> <p>But this does not handle the case where the connection is created by the JRE (JEditorPane example).</p> <p>How can we set the user agent in this case ?</p> <p>We have tried to change the value of <code>sun.net.www.protocol.http.HttpURLConnection.userAgent</code> by using reflection <a href="https://stackoverflow.com/a/3301720/2257172">with this example</a>, but it doesn't work, we're facing an <code>IllegalAccessException</code>.</p> <p>We cannot neither set the User-Agent in the JNLP file as it is not possible to determine client information (user language + platform).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload