mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-17 18:19:52 +00:00
HttpClient: Support custom request headers.
This commit is contained in:
parent
00a395a9ad
commit
05391cab75
1 changed files with 9 additions and 0 deletions
|
@ -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<String, String> headers = new HashMap<String, String>();
|
||||
@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<String, String> entry : headers.entrySet())
|
||||
connection.addRequestProperty(entry.getKey(), entry.getValue());
|
||||
if (userAgent != null)
|
||||
connection.addRequestProperty("User-Agent", userAgent);
|
||||
connection.addRequestProperty("Accept", SCRAPE_ACCEPT);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue