Index: /trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java	(revision 5587)
@@ -1412,5 +1412,5 @@
                             HttpURLConnection conn;
                             for (URI u : uris) {
-                                conn = (HttpURLConnection) u.toURL().openConnection();
+                                conn = Utils.openHttpConnection(u.toURL());
                                 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
 
@@ -1422,8 +1422,8 @@
                                     conn.disconnect();
 
-                                    conn = (HttpURLConnection) new URI(u.toString()
+                                    conn = Utils.openHttpConnection(new URI(u.toString()
                                             .replace("=", "%3D") /* do not URLencode whole string! */
                                             .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
-                                            ).toURL().openConnection();
+                                            ).toURL());
                                     conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
 
Index: /trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/download/PlaceSelection.java	(revision 5587)
@@ -56,4 +56,5 @@
 import org.openstreetmap.josm.tools.ImageProvider;
 import org.openstreetmap.josm.tools.OsmUrlToBounds;
+import org.openstreetmap.josm.tools.Utils;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
@@ -362,5 +363,5 @@
                 URL url = new URL(urlString);
                 synchronized(this) {
-                    connection = (HttpURLConnection)url.openConnection();
+                    connection = Utils.openHttpConnection(url);
                 }
                 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
Index: /trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java	(revision 5587)
@@ -9,4 +9,5 @@
 import java.net.URL;
 
+import org.openstreetmap.josm.tools.Utils;
 import org.openstreetmap.josm.tools.WikiReader;
 
@@ -46,10 +47,8 @@
      */
     public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException {
-        URL url = null;
         HttpURLConnection con = null;
         BufferedReader in = null;
         try {
-            url = new URL(helpTopicUrl);
-            con = (HttpURLConnection)url.openConnection();
+            con = Utils.openHttpConnection(new URL(helpTopicUrl));
             con.connect();
             in = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
Index: /trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java	(revision 5587)
@@ -1,7 +1,4 @@
+// License: GPL. For details, see LICENSE file.
 package org.openstreetmap.josm.gui.io;
-
-
-
-// License: GPL. For details, see LICENSE file.
 
 import static org.openstreetmap.josm.tools.I18n.tr;
@@ -17,11 +14,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
-
-import java.net.URLConnection;
 import java.util.Enumeration;
-
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
-import org.openstreetmap.josm.data.Version;
+
 import org.openstreetmap.josm.gui.PleaseWaitDialog;
 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
@@ -31,6 +25,6 @@
 
 /**
- * Asynchronous task for downloading andnd unpacking arbitrary file lists
- * Shows progress bar when donloading
+ * Asynchronous task for downloading and unpacking arbitrary file lists
+ * Shows progress bar when downloading
  */
 public class DownloadFileTask extends PleaseWaitRunnable{
@@ -63,10 +57,9 @@
 
     private boolean canceled;
-    private URLConnection downloadConnection;
+    private HttpURLConnection downloadConnection;
 
     private synchronized void closeConnectionIfNeeded() {
-        if (downloadConnection != null && downloadConnection instanceof HttpURLConnection) {
-            HttpURLConnection conn = ((HttpURLConnection) downloadConnection);
-            conn.disconnect();
+        if (downloadConnection != null) {
+            downloadConnection.disconnect();
         }
         downloadConnection = null;
@@ -97,8 +90,6 @@
             int size;
             synchronized(this) {
-                downloadConnection = url.openConnection();
+                downloadConnection = Utils.openHttpConnection(url);
                 downloadConnection.setRequestProperty("Cache-Control", "no-cache");
-                downloadConnection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
-                downloadConnection.setRequestProperty("Host", url.getHost());
                 downloadConnection.connect();
                 size = downloadConnection.getContentLength();
@@ -176,5 +167,5 @@
         try {
             zf = new ZipFile(file);
-            Enumeration es = zf.entries();
+            Enumeration<?> es = zf.entries();
             ZipEntry ze;
             while (es.hasMoreElements()) {
Index: /trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java	(revision 5587)
@@ -30,5 +30,4 @@
 
 import org.openstreetmap.josm.Main;
-import org.openstreetmap.josm.data.Version;
 import org.openstreetmap.josm.data.oauth.OAuthParameters;
 import org.openstreetmap.josm.data.oauth.OAuthToken;
@@ -38,4 +37,5 @@
 import org.openstreetmap.josm.io.OsmTransferCanceledException;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -325,10 +325,9 @@
             URL url = new URL(sb.toString());
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
             connection.setRequestMethod("GET");
             connection.setDoInput(true);
             connection.setDoOutput(false);
-            setHttpRequestParameters(connection);
             connection.connect();
             SessionId sessionId = extractOsmSession(connection);
@@ -355,5 +354,5 @@
             URL url = new URL(getAuthoriseUrl(requestToken));
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
             connection.setRequestMethod("GET");
@@ -361,5 +360,4 @@
             connection.setDoOutput(false);
             connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName);
-            setHttpRequestParameters(connection);
             connection.connect();
             sessionId.token = extractToken(connection);
@@ -380,5 +378,5 @@
             URL url = new URL(buildOsmLoginUrl());
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
             connection.setRequestMethod("POST");
@@ -401,5 +399,4 @@
             // make sure we can catch 302 Moved Temporarily below
             connection.setInstanceFollowRedirects(false);
-            setHttpRequestParameters(connection);
 
             connection.connect();
@@ -437,12 +434,11 @@
             URL url = new URL(buildOsmLogoutUrl());
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
             connection.setRequestMethod("GET");
             connection.setDoInput(true);
             connection.setDoOutput(false);
-            setHttpRequestParameters(connection);
             connection.connect();
-        }catch(MalformedURLException e) {
+        } catch(MalformedURLException e) {
             throw new OsmOAuthAuthorizationException(e);
         } catch(IOException e) {
@@ -484,5 +480,5 @@
             URL url = new URL(oauthProviderParameters.getAuthoriseUrl());
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
             connection.setRequestMethod("POST");
@@ -494,5 +490,4 @@
             connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName);
             connection.setInstanceFollowRedirects(false);
-            setHttpRequestParameters(connection);
 
             connection.connect();
@@ -520,9 +515,4 @@
             }
         }
-    }
-
-    protected void setHttpRequestParameters(HttpURLConnection connection) {
-        connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());
-        connection.setRequestProperty("Host", connection.getURL().getHost());
     }
 
Index: /trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java	(revision 5587)
@@ -17,5 +17,4 @@
 import oauth.signpost.exception.OAuthException;
 
-import org.openstreetmap.josm.data.Version;
 import org.openstreetmap.josm.data.oauth.OAuthParameters;
 import org.openstreetmap.josm.data.oauth.OAuthToken;
@@ -30,4 +29,5 @@
 import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.Utils;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
@@ -104,11 +104,9 @@
             DefaultAuthenticator.getInstance().setEnabled(false);
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
             }
 
             connection.setDoOutput(true);
             connection.setRequestMethod("GET");
-            connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());
-            connection.setRequestProperty("Host", connection.getURL().getHost());
             sign(connection);
             connection.connect();
Index: /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java	(revision 5587)
@@ -60,4 +60,5 @@
 import org.openstreetmap.josm.io.UTFInputStreamReader;
 import org.openstreetmap.josm.tools.GBC;
+import org.openstreetmap.josm.tools.Utils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -342,5 +343,5 @@
         try {
             System.out.println("GET "+getCapabilitiesUrl.toString());
-            URLConnection openConnection = getCapabilitiesUrl.openConnection();
+            URLConnection openConnection = Utils.openHttpConnection(getCapabilitiesUrl);
             InputStream inputStream = openConnection.getInputStream();
             BufferedReader br = new BufferedReader(UTFInputStreamReader.create(inputStream, "UTF-8"));
Index: /trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java	(revision 5587)
@@ -20,4 +20,5 @@
 import org.openstreetmap.josm.io.OsmTransferException;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.Utils;
 import org.xml.sax.SAXException;
 
@@ -182,11 +183,9 @@
 
             synchronized(this) {
-                connection = (HttpURLConnection)capabilitiesUrl.openConnection();
+                connection = Utils.openHttpConnection(capabilitiesUrl);
             }
             connection.setDoInput(true);
             connection.setDoOutput(false);
             connection.setRequestMethod("GET");
-            connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());
-            connection.setRequestProperty("Host", connection.getURL().getHost());
             connection.connect();
 
Index: /trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java	(revision 5587)
@@ -269,5 +269,5 @@
         int numRedirects = 0;
         while(true) {
-            con = (HttpURLConnection)downloadUrl.openConnection();
+            con = Utils.openHttpConnection(downloadUrl);
             con.setInstanceFollowRedirects(false);
             con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
Index: /trunk/src/org/openstreetmap/josm/io/OsmApi.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/OsmApi.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/io/OsmApi.java	(revision 5587)
@@ -36,4 +36,5 @@
 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.Utils;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
@@ -585,7 +586,6 @@
                 URL url = new URL(new URL(getBaseUrl()), urlSuffix);
                 System.out.print(requestMethod + " " + url + "... ");
-                activeConnection = (HttpURLConnection)url.openConnection();
                 // fix #5369, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
-                activeConnection.setRequestProperty("Connection", "close");
+                activeConnection = Utils.openHttpConnection(url, false);
                 activeConnection.setConnectTimeout(fastFail ? 1000 : Main.pref.getInteger("socket.timeout.connect",15)*1000);
                 if (fastFail) {
Index: /trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/OsmServerReader.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/io/OsmServerReader.java	(revision 5587)
@@ -19,4 +19,5 @@
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.tools.Utils;
 
 /**
@@ -64,7 +65,6 @@
             }
             try {
-                activeConnection = (HttpURLConnection)url.openConnection();
                 // fix #7640, see http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive
-                activeConnection.setRequestProperty("Connection", "close");
+                activeConnection = Utils.openHttpConnection(url, false);
             } catch(Exception e) {
                 throw new OsmTransferException(tr("Failed to open connection to API {0}.", url.toExternalForm()), e);
Index: /trunk/src/org/openstreetmap/josm/io/imagery/OsmosnimkiOffsetServer.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/imagery/OsmosnimkiOffsetServer.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/io/imagery/OsmosnimkiOffsetServer.java	(revision 5587)
@@ -14,4 +14,5 @@
 import org.openstreetmap.josm.data.imagery.ImageryInfo;
 import org.openstreetmap.josm.data.preferences.StringProperty;
+import org.openstreetmap.josm.tools.Utils;
 
 public class OsmosnimkiOffsetServer implements OffsetServer {
@@ -28,5 +29,5 @@
             URL url = new URL(this.url + "action=CheckAvailability&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
             System.out.println(tr("Querying offset availability: {0}", url));
-            final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
+            final BufferedReader rdr = new BufferedReader(new InputStreamReader(Utils.openHttpConnection(url).getInputStream(), "UTF-8"));
             String response = rdr.readLine();
             System.out.println(tr("Offset server response: {0}", response));
@@ -46,5 +47,5 @@
             URL url = new URL(this.url + "action=GetOffsetForPoint&lat=" + ll.lat() + "&lon=" + ll.lon() + "&id=" + URLEncoder.encode(info.getUrl(), "UTF-8"));
             System.out.println(tr("Querying offset: {0}", url.toString()));
-            final BufferedReader rdr = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8"));
+            final BufferedReader rdr = new BufferedReader(new InputStreamReader(Utils.openHttpConnection(url).getInputStream(), "UTF-8"));
             String s = rdr.readLine();
             if (s == null)
Index: /trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java	(revision 5587)
@@ -162,5 +162,5 @@
         System.out.println("Grabbing WMS " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
 
-        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        HttpURLConnection conn = Utils.openHttpConnection(url);
         for(Entry<String, String> e : props.entrySet()) {
             conn.setRequestProperty(e.getKey(), e.getValue());
Index: /trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java	(revision 5587)
@@ -119,8 +119,6 @@
             URL url = new URL(pi.downloadlink);
             synchronized(this) {
-                downloadConnection = (HttpURLConnection)url.openConnection();
+                downloadConnection = Utils.openHttpConnection(url);
                 downloadConnection.setRequestProperty("Cache-Control", "no-cache");
-                downloadConnection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
-                downloadConnection.setRequestProperty("Host", url.getHost());
                 downloadConnection.connect();
             }
Index: /trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java	(revision 5587)
@@ -162,8 +162,6 @@
             URL url = new URL(site);
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
                 connection.setRequestProperty("Cache-Control", "no-cache");
-                connection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
-                connection.setRequestProperty("Host", url.getHost());
                 connection.setRequestProperty("Accept-Charset", "utf-8");
             }
@@ -211,8 +209,6 @@
             URL url = new URL(site);
             synchronized(this) {
-                connection = (HttpURLConnection)url.openConnection();
+                connection = Utils.openHttpConnection(url);
                 connection.setRequestProperty("Cache-Control", "no-cache");
-                connection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
-                connection.setRequestProperty("Host", url.getHost());
             }
             in = connection.getInputStream();
Index: /trunk/src/org/openstreetmap/josm/tools/Utils.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/tools/Utils.java	(revision 5586)
+++ /trunk/src/org/openstreetmap/josm/tools/Utils.java	(revision 5587)
@@ -16,4 +16,6 @@
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -25,4 +27,6 @@
 import java.util.Iterator;
 import java.util.List;
+
+import org.openstreetmap.josm.data.Version;
 
 /**
@@ -533,4 +537,35 @@
         return new Color(Integer.parseInt(clr, 16));
     }
-
+    
+    /**
+     * Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one.
+     * @param httpURL The HTTP url to open (must use http:// or https://)
+     * @return An open HTTP connection to the given URL
+     * @throws IOException if an I/O exception occurs.
+     * @since 5587
+     */
+    public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {
+        if (httpURL == null || !httpURL.getProtocol().matches("https?")) {
+            throw new IllegalArgumentException("Invalid HTTP url");
+        }
+        HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
+        connection.setRequestProperty("User-Agent", Version.getInstance().getAgentString());
+        return connection;
+    }
+    
+    /**
+     * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.
+     * @param httpURL The HTTP url to open (must use http:// or https://)
+     * @param keepAlive 
+     * @return An open HTTP connection to the given URL
+     * @throws IOException if an I/O exception occurs.
+     * @since 5587
+     */
+    public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive) throws IOException {
+        HttpURLConnection connection = openHttpConnection(httpURL);
+        if (!keepAlive) {
+            connection.setRequestProperty("Connection", "close");
+        }
+        return connection;
+    }
 }
