From a52b981f320437dcb72166c0d6ac590d40315f4a Mon Sep 17 00:00:00 2001 From: feilong Date: Thu, 17 Sep 2020 15:00:29 +0800 Subject: [PATCH] =?UTF-8?q?http=20=E5=B7=A5=E5=85=B7=E7=B1=BB,=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=8A=A8=E5=8E=BB=E9=99=A4url=20=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC,=20=E6=8F=90=E9=AB=98=E5=81=A5=E5=A3=AE=E6=80=A7=20fi?= =?UTF-8?q?x=20#291?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/feilong/net/http/HttpRequest.java | 59 +++++++++++++++++++ .../com/feilong/net/http/HttpRequestTest.java | 13 ++++ 2 files changed, 72 insertions(+) diff --git a/feilong-net/feilong-net-api/src/main/java/com/feilong/net/http/HttpRequest.java b/feilong-net/feilong-net-api/src/main/java/com/feilong/net/http/HttpRequest.java index c78abef01..be7de6293 100644 --- a/feilong-net/feilong-net-api/src/main/java/com/feilong/net/http/HttpRequest.java +++ b/feilong-net/feilong-net-api/src/main/java/com/feilong/net/http/HttpRequest.java @@ -69,6 +69,20 @@ public class HttpRequest{ //--------------------------------------------------------------- + /** + * 是否uri trim. + *

+ * 有时候程序员在配置uri 的时候,会误操作 uri 前后会多出空格, 这样会导致一些功能不work,要排查好久,
+ * 理论上uri前后是没有空格的, 所以默认为true ,会自动去除前后的空格;
+ * 如果真的有特殊需求, 可以设置为false + *

+ * + * @since 3.0.10 + */ + private boolean isTrimUri = true; + + //--------------------------------------------------------------- + /** * The Constructor. * @@ -172,6 +186,16 @@ public String getUri(){ if (isNullOrEmpty(uri)){ return EMPTY; } + + //--------------------------------------------------------------- + + //since 3.0.10 + if (isTrimUri){ + uri = uri.trim(); + } + + //--------------------------------------------------------------- + //since 1.14.0 if (uri.contains(SPACE)){ //W3C标准规定, 当Content-Type为application/x-www-form-urlencoded时,URL中查询参数名和参数值中空格要用加号+替代, @@ -283,4 +307,39 @@ public String getRequestBody(){ public void setRequestBody(String requestBody){ this.requestBody = requestBody; } + + //--------------------------------------------------------------- + + /** + * 是否uri trim. + * + *

+ * 有时候程序员在配置uri 的时候,会误操作 uri 前后会多出空格, 这样会导致一些功能不work,要排查好久,
+ * 理论上uri前后是没有空格的, 所以默认为true ,会自动去除前后的空格;
+ * 如果真的有特殊需求, 可以设置为false + *

+ * + * @param isTrimUri + * the isTrimUri to set + * @since 3.0.10 + */ + public void setIsTrimUri(boolean isTrimUri){ + this.isTrimUri = isTrimUri; + } + + /** + * 是否uri trim. + * + *

+ * 有时候程序员在配置uri 的时候,会误操作 uri 前后会多出空格, 这样会导致一些功能不work,要排查好久,
+ * 理论上uri前后是没有空格的, 所以默认为true ,会自动去除前后的空格;
+ * 如果真的有特殊需求, 可以设置为false + *

+ * + * @return the isTrimUri + * @since 3.0.10 + */ + public boolean getIsTrimUri(){ + return isTrimUri; + } } diff --git a/feilong-net/feilong-net-api/src/test/java/com/feilong/net/http/HttpRequestTest.java b/feilong-net/feilong-net-api/src/test/java/com/feilong/net/http/HttpRequestTest.java index 3e0c94192..73a8d3ead 100644 --- a/feilong-net/feilong-net-api/src/test/java/com/feilong/net/http/HttpRequestTest.java +++ b/feilong-net/feilong-net-api/src/test/java/com/feilong/net/http/HttpRequestTest.java @@ -39,4 +39,17 @@ public void test12(){ HttpRequest httpRequest = new HttpRequest("https://github.com/ifei long/fei long"); assertEquals("https://github.com/ifei%20long/fei%20long", httpRequest.getUri()); } + + @Test + public void test1222(){ + HttpRequest httpRequest = new HttpRequest("https://github.com/ifeilong/fei long "); + assertEquals("https://github.com/ifeilong/fei%20long", httpRequest.getUri()); + } + + @Test + public void test12333(){ + HttpRequest httpRequest = new HttpRequest("https://github.com/ifei long/fei long "); + httpRequest.setIsTrimUri(false); + assertEquals("https://github.com/ifei%20long/fei%20long%20", httpRequest.getUri()); + } }