Skip to content

Commit

Permalink
http 工具类, 支持自动去除url 空格, 提高健壮性 fix #291
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Sep 17, 2020
1 parent 8ce7091 commit a52b981
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public class HttpRequest{

//---------------------------------------------------------------

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

//---------------------------------------------------------------

/**
* The Constructor.
*
Expand Down Expand Up @@ -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中查询参数名和参数值中空格要用加号+替代,
Expand Down Expand Up @@ -283,4 +307,39 @@ public String getRequestBody(){
public void setRequestBody(String requestBody){
this.requestBody = requestBody;
}

//---------------------------------------------------------------

/**
* 是否uri trim.
*
* <p>
* 有时候程序员在配置uri 的时候,会误操作 uri 前后会多出空格, 这样会导致一些功能不work,要排查好久, <br>
* 理论上uri前后是没有空格的, 所以默认为true ,会自动去除前后的空格; <br>
* 如果真的有特殊需求, 可以设置为false
* </p>
*
* @param isTrimUri
* the isTrimUri to set
* @since 3.0.10
*/
public void setIsTrimUri(boolean isTrimUri){
this.isTrimUri = isTrimUri;
}

/**
* 是否uri trim.
*
* <p>
* 有时候程序员在配置uri 的时候,会误操作 uri 前后会多出空格, 这样会导致一些功能不work,要排查好久, <br>
* 理论上uri前后是没有空格的, 所以默认为true ,会自动去除前后的空格; <br>
* 如果真的有特殊需求, 可以设置为false
* </p>
*
* @return the isTrimUri
* @since 3.0.10
*/
public boolean getIsTrimUri(){
return isTrimUri;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit a52b981

Please sign in to comment.