From 05391cab7580765022f13bfd79bdb472989cc920 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 19 Jul 2015 14:34:16 +0200 Subject: [PATCH] HttpClient: Support custom request headers. --- enabler/src/de/schildbach/pte/util/HttpClient.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/enabler/src/de/schildbach/pte/util/HttpClient.java b/enabler/src/de/schildbach/pte/util/HttpClient.java index 107e60eb..275791a9 100644 --- a/enabler/src/de/schildbach/pte/util/HttpClient.java +++ b/enabler/src/de/schildbach/pte/util/HttpClient.java @@ -28,6 +28,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; @@ -54,6 +55,7 @@ public final class HttpClient { @Nullable private String userAgent = null; + private Map headers = new HashMap(); @Nullable private String sessionCookieName = null; @Nullable @@ -73,6 +75,11 @@ public final class HttpClient this.userAgent = userAgent; } + public void setHeader(final String headerName, final String headerValue) + { + this.headers.put(headerName, headerValue); + } + public void setSessionCookieName(final String sessionCookieName) { this.sessionCookieName = sessionCookieName; @@ -136,6 +143,8 @@ public final class HttpClient connection.setDoOutput(postRequest != null); connection.setConnectTimeout(SCRAPE_CONNECT_TIMEOUT); connection.setReadTimeout(SCRAPE_READ_TIMEOUT); + for (final Map.Entry entry : headers.entrySet()) + connection.addRequestProperty(entry.getKey(), entry.getValue()); if (userAgent != null) connection.addRequestProperty("User-Agent", userAgent); connection.addRequestProperty("Accept", SCRAPE_ACCEPT);