From 65990cb8a061f1991cdc00fe4f4225d6dda79bae Mon Sep 17 00:00:00 2001 From: Stephan Fuhrmann Date: Fri, 12 Jan 2024 22:08:50 +0100 Subject: [PATCH] Add both x-www-form-urlencoded and json for now --- .../RestDelegateUrlConnectionImpl.java | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/radiobrowser4j/src/main/java/de/sfuhrm/radiobrowser4j/RestDelegateUrlConnectionImpl.java b/radiobrowser4j/src/main/java/de/sfuhrm/radiobrowser4j/RestDelegateUrlConnectionImpl.java index b622f160..544e647c 100644 --- a/radiobrowser4j/src/main/java/de/sfuhrm/radiobrowser4j/RestDelegateUrlConnectionImpl.java +++ b/radiobrowser4j/src/main/java/de/sfuhrm/radiobrowser4j/RestDelegateUrlConnectionImpl.java @@ -195,6 +195,22 @@ private String asApplicationJson( return json; } + private static String asWwwFormUrlEncoded( + final Map requestParams + ) throws UnsupportedEncodingException { + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry : requestParams.entrySet()) { + if (sb.length() > 0) { + sb.append("&"); + } + sb.append(URLEncoder.encode(entry.getKey(), "UTF-8")); + sb.append("="); + sb.append(URLEncoder.encode(entry.getValue(), "UTF-8")); + } + return sb.toString(); + } + + /** Sends a POST request to the remote server. The * body gets transferred as * "application/x-www-form-urlencoded" encoded data. @@ -211,18 +227,16 @@ private T post(final String path, final Map requestParams, final TypeToken resultClass) { try { - String requestBody = asApplicationJson(requestParams); - log.debug("POST body: {}", requestBody); HttpURLConnection connection = newClient(path); configure(connection); connection.setRequestMethod("POST"); - connection.setRequestProperty( - "Content-Type", - "application/json; charset=UTF-8"); connection.setDoOutput(true); - connection.getOutputStream() - .write(requestBody.getBytes(StandardCharsets.UTF_8)); + if (false) { + sendJsonRequest(connection, requestParams); + } else { + sendXWWWFormUrlencodedRequest(connection, requestParams); + } try (Reader reader = readerFor(connection)) { checkResponseStatus(connection); return gson.fromJson(reader, resultClass); @@ -234,6 +248,30 @@ private T post(final String path, } } + private void sendXWWWFormUrlencodedRequest(final HttpURLConnection connection, + final Map requestParams) throws IOException { + connection.setRequestProperty( + "Content-Type", + "application/x-www-form-urlencoded; charset=UTF-8"); + String encoded = asWwwFormUrlEncoded(requestParams); + log.debug("POST WWW-Form-UrlEncoded body: {}", encoded); + connection.getOutputStream() + .write(encoded.getBytes(StandardCharsets.UTF_8)); + connection.getOutputStream().flush(); + } + + private void sendJsonRequest(final HttpURLConnection connection, + final Map requestParams) throws IOException { + String json = asApplicationJson(requestParams); + log.debug("POST JSON body: {}", json); + connection.setRequestProperty( + "Content-Type", + "application/json; charset=UTF-8"); + connection.getOutputStream() + .write(json.getBytes(StandardCharsets.UTF_8)); + connection.getOutputStream().flush(); + } + @Override public T post(final String path, final Map requestParams,