Index: applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
===================================================================
--- applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java	(revision 31866)
+++ applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java	(revision 31881)
@@ -4,7 +4,6 @@
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStreamWriter;
-import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.AbstractList;
 import java.util.ArrayList;
@@ -20,5 +19,8 @@
 import java.util.regex.Pattern;
 
+import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
@@ -30,4 +32,5 @@
 import org.openstreetmap.josm.data.osm.Tag;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.HttpClient;
 import org.openstreetmap.josm.tools.Predicate;
 import org.openstreetmap.josm.tools.Utils;
@@ -40,4 +43,6 @@
 
     public static Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+");
+    private static final DocumentBuilder DOCUMENT_BUILDER = newDocumentBuilder();
+    private static final XPath X_PATH = XPathFactory.newInstance().newXPath();
 
     private WikipediaApp() {
@@ -50,12 +55,11 @@
             final String url = "https://tools.wmflabs.org/wp-world/marks.php?"
                     + "bbox=" + bbox + "&LANG=" + wikipediaLang;
-            Main.info("Wikipedia: GET " + url);
             // parse XML document
-            final XPathExpression xpathPlacemark = XPathFactory.newInstance().newXPath().compile("//Placemark");
-            final XPathExpression xpathName = XPathFactory.newInstance().newXPath().compile("name/text()");
-            final XPathExpression xpathCoord = XPathFactory.newInstance().newXPath().compile("Point/coordinates/text()");
-            final XPathExpression xpathDescr = XPathFactory.newInstance().newXPath().compile("description");
-            try (final InputStream in = Utils.openURL(new URL(url))) {
-                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
+            final XPathExpression xpathPlacemark = X_PATH.compile("//Placemark");
+            final XPathExpression xpathName = X_PATH.compile("name/text()");
+            final XPathExpression xpathCoord = X_PATH.compile("Point/coordinates/text()");
+            final XPathExpression xpathDescr = X_PATH.compile("description");
+            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
+                Document doc = DOCUMENT_BUILDER.parse(in);
                 NodeList nodes = (NodeList) xpathPlacemark.evaluate(doc, XPathConstants.NODESET);
                 // construct WikipediaEntry for each XML element
@@ -84,7 +88,8 @@
                     + "&depth=" + depth
                     + "&cat=" + Utils.encodeUrl(category);
-            Main.info("Wikipedia: GET " + url);
-            try (final InputStream in = Utils.openURL(new URL(url));
-                 final Scanner scanner = new Scanner(in, "UTF-8").useDelimiter("\n")) {
+
+            try (final Scanner scanner = new Scanner(
+                    HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader())
+                    .useDelimiter("\n")) {
                 final List<WikipediaEntry> entries = new ArrayList<>();
                 while (scanner.hasNext()) {
@@ -119,15 +124,12 @@
             final String url = "https://tools.wmflabs.org/wiwosm/osmjson/getGeoJSON.php?action=check"
                     + "&lang=" + wikipediaLang;
-            Main.info("Wikipedia: POST " + url + " " + articleNames);
 
             try {
-                final HttpURLConnection connection = Utils.openHttpConnection(new URL(url));
-                connection.setDoOutput(true);
-
-                try (final OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) {
-                    out.write("articles=" + Utils.encodeUrl(Utils.join(",", articleNames)));
-                }
-
-                try (final Scanner scanner = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\n")) {
+                final String requestBody = "articles=" + Utils.encodeUrl(Utils.join(",", articleNames));
+                try (final Scanner scanner = new Scanner(
+                        HttpClient.create(new URL(url), "PUT").setReasonForRequest("Wikipedia")
+                                .setRequestBody(requestBody.getBytes(StandardCharsets.UTF_8))
+                                .connect().getContentReader())
+                        .useDelimiter("\n")) {
                     while (scanner.hasNext()) {
                         //[article]\t[0|1]
@@ -186,13 +188,12 @@
                 }
             }));
-            Main.info("Wikipedia: GET " + url);
             final Map<String, String> r = new TreeMap<>();
-            try (final InputStream in = Utils.openURL(new URL(url))) {
-                final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
-                final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//entity").evaluate(xml, XPathConstants.NODESET);
+            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
+                final Document xml = DOCUMENT_BUILDER.parse(in);
+                final NodeList nodes = (NodeList) X_PATH.compile("//entity").evaluate(xml, XPathConstants.NODESET);
                 for (int i = 0; i < nodes.getLength(); i++) {
                     final Node node = nodes.item(i);
-                    final String wikidata = (String) XPathFactory.newInstance().newXPath().compile("./@id").evaluate(node, XPathConstants.STRING);
-                    final String wikipedia = (String) XPathFactory.newInstance().newXPath().compile("./sitelinks/sitelink/@title").evaluate(node, XPathConstants.STRING);
+                    final String wikidata = (String) X_PATH.compile("./@id").evaluate(node, XPathConstants.STRING);
+                    final String wikipedia = (String) X_PATH.compile("./sitelinks/sitelink/@title").evaluate(node, XPathConstants.STRING);
                     r.put(wikipedia, wikidata);
                 }
@@ -213,8 +214,7 @@
                     "&format=xml" +
                     (preferredLanguage != null ? "&languages=" + preferredLanguage + "&languagefallback=en" : "");
-            Main.info("Wikipedia: GET " + url);
-            try (final InputStream in = Utils.openURL(new URL(url))) {
-                final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
-                final Node label = (Node) XPathFactory.newInstance().newXPath().compile("//label").evaluate(xml, XPathConstants.NODE);
+            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
+                final Document xml = DOCUMENT_BUILDER.parse(in);
+                final Node label = (Node) X_PATH.compile("//label").evaluate(xml, XPathConstants.NODE);
                 if (label == null && preferredLanguage != null) {
                     return getLabelForWikidata(wikidataId, null);
@@ -222,5 +222,5 @@
                     return null;
                 } else {
-                    return (String) XPathFactory.newInstance().newXPath().compile("./@value").evaluate(label, XPathConstants.STRING);
+                    return (String) X_PATH.compile("./@value").evaluate(label, XPathConstants.STRING);
                 }
             }
@@ -239,8 +239,7 @@
                     "&lllimit=500" +
                     "&format=xml";
-            Main.info("Wikipedia: GET " + url);
-            try (final InputStream in = Utils.openURL(new URL(url))) {
-                final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
-                final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//ll").evaluate(xml, XPathConstants.NODESET);
+            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
+                final Document xml = DOCUMENT_BUILDER.parse(in);
+                final NodeList nodes = (NodeList) X_PATH.compile("//ll").evaluate(xml, XPathConstants.NODESET);
                 for (int i = 0; i < nodes.getLength(); i++) {
                     final String lang = nodes.item(i).getAttributes().getNamedItem("lang").getTextContent();
@@ -370,7 +369,6 @@
                         + "&lang=" + wikipediaLang
                         + "&article=" + Utils.encodeUrl(wikipediaArticle);
-                Main.info("Wikipedia: GET " + url);
-                try (final InputStream in = Utils.openURL(new URL(url));
-                     final Scanner scanner = new Scanner(in, "UTF-8")) {
+                try (final Scanner scanner = new Scanner(
+                        HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContentReader())) {
                     wiwosmStatus = scanner.hasNextInt() && scanner.nextInt() == 1;
                 }
@@ -419,3 +417,13 @@
         };
     }
+
+    private static DocumentBuilder newDocumentBuilder() {
+        try {
+            return DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        } catch (ParserConfigurationException e) {
+            Main.warn("Cannot create DocumentBuilder");
+            Main.warn(e);
+            throw new RuntimeException(e);
+        }
+    }
 }
