Tuesday, September 17, 2013

Setting Processing's User-Agent for loadXML

In Processing, I'm fetching Yahoo's weather forecast for my location.  The loadXML() function is perfectly happy to take a URL, so I can just point it at my location's RSS feed.

In its default mode, this sends a user-agent string of "Java/1.6.0_51" which reflects my current Java version.  But I want to set my User-Agent, to something more descriptive.  If my project misbehaves, or an operator gets curious, it would be polite to have a more descriptive string with some contact info.

You can prepend a string to the user-agent by specifying it in your setup() function.

void setup () {
  System.setProperty("http.agent", "abstractForecast; https://github.com/fnaard/abstractForecast;");

Which results in the web server logging a user-agent of:

abstractForecast; https://github.com/fnaard/abstractForecast; Java/1.6.0_51

Oracle has some documentation on the network properties you can set.  There are more, notably: proxy settings, IPv4/v6 preference, and DNS caching behavior.

No comments:

Post a Comment