From 06091193c72b128519dc6b51617662ce0aac6272 Mon Sep 17 00:00:00 2001 From: Hunter <53144338+hunter-cloud09@users.noreply.github.com> Date: Sat, 9 Sep 2023 16:39:27 +0800 Subject: [PATCH] cherry-pick fix create http request error #14793 --- .../plugin/alert/http/HttpSender.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java index 017719c2f7..f707195051 100644 --- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java +++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java @@ -34,10 +34,13 @@ import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; @@ -104,13 +107,13 @@ public final class HttpSender { } private void createHttpRequest(String msg) throws MalformedURLException, URISyntaxException { - if (REQUEST_TYPE_POST.equals(requestType)) { + if (REQUEST_TYPE_POST.equalsIgnoreCase(requestType)) { httpRequest = new HttpPost(url); setHeader(); //POST request add param in request body setMsgInRequestBody(msg); - } else if (REQUEST_TYPE_GET.equals(requestType)) { - //GET request add param in url + } else if (REQUEST_TYPE_GET.equalsIgnoreCase(requestType)) { + // GET request add param in url setMsgInUrl(msg); URL unencodeUrl = new URL(url); URI uri = new URI(unencodeUrl.getProtocol(), unencodeUrl.getAuthority(), unencodeUrl.getPath(), @@ -132,7 +135,11 @@ public final class HttpSender { if (!url.contains(URL_SPLICE_CHAR)) { type = URL_SPLICE_CHAR; } - url = String.format("%s%s%s=%s", url, type, contentField, msg); + try { + url = String.format("%s%s%s=%s", url, type, contentField, URLEncoder.encode(msg, DEFAULT_CHARSET)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } }