Index: /applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/CadastreInterface.java
===================================================================
--- /applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/CadastreInterface.java	(revision 33964)
+++ /applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/wms/CadastreInterface.java	(revision 33965)
@@ -5,9 +5,6 @@
 
 import java.awt.GridBagLayout;
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
 import java.net.CookieHandler;
 import java.net.HttpURLConnection;
@@ -20,4 +17,5 @@
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map.Entry;
 
 import javax.swing.JComboBox;
@@ -31,15 +29,15 @@
 import org.openstreetmap.josm.gui.MainApplication;
 import org.openstreetmap.josm.gui.layer.Layer;
-import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
 import org.openstreetmap.josm.gui.util.GuiHelper;
 import org.openstreetmap.josm.io.OsmTransferException;
-import org.openstreetmap.josm.io.ProgressInputStream;
 import org.openstreetmap.josm.plugins.fr.cadastre.CadastrePlugin;
 import org.openstreetmap.josm.tools.GBC;
+import org.openstreetmap.josm.tools.HttpClient;
+import org.openstreetmap.josm.tools.HttpClient.Response;
 import org.openstreetmap.josm.tools.Logging;
 
 public class CadastreInterface {
     public boolean downloadCanceled;
-    private HttpURLConnection urlConn;
+    private Response urlConn;
 
     private String csrfToken;
@@ -130,14 +128,8 @@
             searchFormURL = new URL(BASE_URL + "/scpc/accueil.do");
             while (!success && retries > 0) {
-                urlConn = (HttpURLConnection) searchFormURL.openConnection();
-                urlConn.setRequestProperty("Connection", "close");
-                urlConn.setRequestMethod("GET");
-                urlConn.connect();
+                urlConn = getHttpClient(searchFormURL).setHeader("Connection", "close").connect();
                 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
-                    Logging.info("GET "+searchFormURL);
-                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
-                    while (in.readLine() != null) {
-                        // read the buffer otherwise we sent POST too early
-                    }
+                    // read the buffer otherwise we sent POST too early
+                    urlConn.fetchContent();
                     success = true;
                     // See https://bugs.openjdk.java.net/browse/JDK-8036017
@@ -149,7 +141,7 @@
                         }
                     } else {
-                        String headerName;
-                        for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
-                            if ("Set-Cookie".equals(headerName) && handleCookie(urlConn.getHeaderField(i))) {
+                        for (Entry<String, List<String>> e : urlConn.getHeaderFields().entrySet()) {
+                            if ("Set-Cookie".equals(e.getKey()) && e.getValue() != null && !e.getValue().isEmpty()
+                                    && handleCookie(e.getValue().get(0))) {
                                 break;
                             }
@@ -204,10 +196,10 @@
     }
 
-    private void setCookie() {
-        this.urlConn.setRequestProperty("Cookie", this.cookie);
-    }
-
-    public void setCookie(HttpURLConnection urlConn) {
-        urlConn.setRequestProperty("Cookie", this.cookie);
+    public HttpClient getHttpClient(URL url) {
+        return HttpClient.create(url).setHeader("Cookie", cookie);
+    }
+
+    public HttpClient getHttpClient(URL url, String method) {
+        return HttpClient.create(url, method).setHeader("Cookie", cookie);
     }
 
@@ -255,21 +247,10 @@
             // finally, open the interface on server side giving access to the wms server
             URL interfaceURL = new URL(BASE_URL + "/scpc/"+interfaceRef);
-            urlConn = (HttpURLConnection) interfaceURL.openConnection();
-            urlConn.setRequestMethod("GET");
-            setCookie();
-            urlConn.connect();
+            urlConn = getHttpClient(interfaceURL).connect();
             if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                 throw new IOException("Cannot open Cadastre interface. GET response:"+urlConn.getResponseCode());
             }
-            Logging.info("GET "+interfaceURL);
-            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8));
             // read the buffer otherwise we sent POST too early
-            StringBuilder lines = new StringBuilder();
-            String ln;
-            while ((ln = in.readLine()) != null) {
-                if (Logging.isDebugEnabled()) {
-                    lines.append(ln);
-                }
-            }
+            String lines = urlConn.fetchContent();
             if (Logging.isDebugEnabled()) {
                 Logging.debug(lines.toString());
@@ -318,22 +299,6 @@
             content += "&x=0&y=0";
             searchFormURL = new URL(BASE_URL + "/scpc/rechercherPlan.do");
-            urlConn = (HttpURLConnection) searchFormURL.openConnection();
-            urlConn.setRequestMethod("POST");
-            urlConn.setDoOutput(true);
-            urlConn.setDoInput(true);
-            setCookie();
-            try (OutputStream wr = urlConn.getOutputStream()) {
-                wr.write(content.getBytes(StandardCharsets.UTF_8));
-                Logging.info("POST "+content);
-                wr.flush();
-            }
-            String ln;
-            StringBuilder sb = new StringBuilder();
-            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
-                while ((ln = rd.readLine()) != null) {
-                    sb.append(ln);
-                }
-            }
-            String lines = sb.toString();
+            urlConn = getHttpClient(searchFormURL, "POST").setRequestBody(content.getBytes(StandardCharsets.UTF_8)).connect();
+            String lines = urlConn.fetchContent();
             urlConn.disconnect();
             if (lines != null) {
@@ -410,25 +375,13 @@
     private String getFeuillesList(String csrfToken) {
         // get all images in one html page
-        String ln = null;
-        StringBuilder lines = new StringBuilder();
-        HttpURLConnection urlConn2 = null;
         try {
             URL getAllImagesURL = new URL(BASE_URL + "/scpc/listerFeuillesParcommune.do?CSRF_TOKEN=" +
                     csrfToken + "&keepVolatileSession=&offset=2000");
-            urlConn2 = (HttpURLConnection) getAllImagesURL.openConnection();
-            setCookie(urlConn2);
-            urlConn2.connect();
-            Logging.info("GET "+getAllImagesURL);
-            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream(), StandardCharsets.UTF_8))) {
-                while ((ln = rd.readLine()) != null) {
-                    lines.append(ln);
-                }
-            }
-            urlConn2.disconnect();
+            return getHttpClient(getAllImagesURL).connect().fetchContent();
         } catch (IOException e) {
             listOfFeuilles.clear();
             Logging.error(e);
         }
-        return lines.toString();
+        return "";
     }
 
@@ -518,24 +471,6 @@
             return;
         // send GET opening normally the small window with the commune overview
-        String content = BASE_URL + "/scpc/" + interfaceRef;
-        content += "&dontSaveLastForward&keepVolatileSession=";
-        searchFormURL = new URL(content);
-        urlConn = (HttpURLConnection) searchFormURL.openConnection();
-        urlConn.setRequestMethod("GET");
-        setCookie();
-        urlConn.connect();
-        if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
-            throw new IOException("Cannot get Cadastre response.");
-        }
-        Logging.info("GET "+searchFormURL);
-        String ln;
-        StringBuilder sb = new StringBuilder();
-        try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8))) {
-            while ((ln = in.readLine()) != null) {
-                sb.append(ln);
-            }
-        }
-        urlConn.disconnect();
-        String line = sb.toString();
+        searchFormURL = new URL(BASE_URL + "/scpc/" + interfaceRef + "&dontSaveLastForward&keepVolatileSession=");
+        String line = getHttpClient(searchFormURL).connect().fetchContent();
         parseBBoxCommune(wmsLayer, line);
         if (wmsLayer.isRaster() && !wmsLayer.isAlreadyGeoreferenced()) {
@@ -610,6 +545,5 @@
     public void cancel() {
         if (urlConn != null) {
-            urlConn.setConnectTimeout(1);
-            urlConn.setReadTimeout(1);
+            urlConn.disconnect();
         }
         downloadCanceled = true;
@@ -618,9 +552,6 @@
 
     public InputStream getContent(URL url) throws IOException, OsmTransferException {
-        urlConn = (HttpURLConnection) url.openConnection();
-        urlConn.setRequestProperty("Connection", "close");
-        urlConn.setRequestMethod("GET");
-        setCookie();
-        return new ProgressInputStream(urlConn, NullProgressMonitor.INSTANCE);
+        urlConn = getHttpClient(url).setHeader("Connection", "close").connect();
+        return urlConn.getContent();
     }
 }
