Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
HttpClient uri 中如果有空格会报错 fix #66
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Jun 26, 2019
1 parent 03f40dd commit d992c96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package com.feilong.net.entity;

import static com.feilong.core.CharsetType.UTF8;
import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.net.HttpMethodType.GET;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.SPACE;

import java.util.Map;

Expand Down Expand Up @@ -146,15 +149,35 @@ public HttpRequest(String uri, Map<String, String> paramMap, String httpMethodTy
* @return the full encoded url
*/
public String getFullEncodedUrl(){
return ParamUtil.addParameterSingleValueMap(uri, paramMap, UTF8);
return ParamUtil.addParameterSingleValueMap(getUri(), paramMap, UTF8);
}

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

/**
* 获得 请求的uri地址.
*
* @return the 请求的uri地址
* @see <a href="https://github.com/venusdrogon/feilong-platform/issues/257">增加自动转义-请求参数放在路径中的配置</a>
* @see <a href="https://github.com/venusdrogon/feilong-net/issues/66">HttpClient uri 中如果有空格会报错</a>
*/
public String getUri(){
//这种改造 系统工作量最小
//since 1.14.0
if (isNullOrEmpty(uri)){
return EMPTY;
}
//since 1.14.0
if (uri.contains(SPACE)){
//W3C标准规定, 当Content-Type为application/x-www-form-urlencoded时,URL中查询参数名和参数值中空格要用加号+替代,
//所以几乎所有使用该规范的浏览器在表单提交后,URL查询参数中空格都会被编成加号+。

//而在另一份规范(RFC 2396,定义URI)里, URI里的保留字符都需转义成%HH格式(Section 3.4 Query Component),因此空格会被编码成%20,加号+本身也作为保留字而被编成%2B,
//对于某些遵循RFC 2396标准的应用来说,它可能不接受查询字符串中出现加号+,认为它是非法字符。

//所以一个安全的举措是URL中统一使用%20来编码空格字符。
uri = uri.replaceAll(SPACE, "%20");
}
return uri;
}

Expand All @@ -168,6 +191,8 @@ public void setUri(String uri){
this.uri = uri;
}

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

/**
* 获得 请求method 类型,默认 {@link HttpMethodType#GET}.
*
Expand All @@ -187,6 +212,8 @@ public void setHttpMethodType(HttpMethodType httpMethodType){
this.httpMethodType = httpMethodType;
}

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

/**
* 获得 请求参数.
*
Expand All @@ -206,6 +233,8 @@ public void setParamMap(Map<String, String> paramMap){
this.paramMap = paramMap;
}

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

/**
* 获得 请求头 信息.
*
Expand All @@ -225,6 +254,8 @@ public void setHeaderMap(Map<String, String> headerMap){
this.headerMap = headerMap;
}

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

/**
* 获得 请求正文,比如 webservice 可以传递 xml/json数据体.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private URIBuilderBuilder(){
*/
static URIBuilder build(HttpRequest httpRequest){
try{
URIBuilder uriBuilder = new URIBuilder(httpRequest.getUri());
String uri = httpRequest.getUri();
URIBuilder uriBuilder = new URIBuilder(uri);

Map<String, String> paramMap = httpRequest.getParamMap();
if (isNullOrEmpty(paramMap)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

PutTest.class,
PutWithParamsTest.class,

HttpClientUtilUriSpaceTest.class,
//
})
public class FeiLongHttpclient4SuiteTests{
Expand Down

0 comments on commit d992c96

Please sign in to comment.