Skip to content

Commit

Permalink
Only merge cookies together but not other headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Sep 23, 2024
1 parent cb48b8c commit be1e6d4
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.lenni0451.commons.httpclient.utils;

import lombok.experimental.UtilityClass;
import net.lenni0451.commons.httpclient.constants.Headers;

import javax.annotation.Nullable;
import javax.annotation.WillNotClose;
Expand Down Expand Up @@ -73,14 +74,28 @@ public static void updateCookies(@Nullable final CookieManager cookieManager, fi
}

/**
* Set the headers for a connection.
* Set the headers for a connection.<br>
* Cookies will be merged into one header separated by {@code ;}.
*
* @param connection The connection to set the headers for
* @param headers The headers to set
*/
public static void setHeaders(final HttpURLConnection connection, final Map<String, List<String>> headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), String.join("; ", entry.getValue()));
if (Headers.COOKIE.equalsIgnoreCase(entry.getKey())) {
connection.setRequestProperty(entry.getKey(), String.join("; ", entry.getValue()));
} else {
boolean first = true;
for (String val : entry.getValue()) {
if (first) {
first = false;
//Use the first value to clear all previous values
connection.setRequestProperty(entry.getKey(), val);
} else {
connection.addRequestProperty(entry.getKey(), val);
}
}
}
}
}

Expand Down

0 comments on commit be1e6d4

Please sign in to comment.