Skip to content

Commit 76d312c

Browse files
author
崔成龙
committed
修复post请求时发送json,而content-type为text/plain的bug;
更新版本为1.0.2
1 parent c12aa42 commit 76d312c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.arronlong</groupId>
55
<artifactId>httpclientutil</artifactId>
6-
<version>1.0.2-SNAPSHOT</version>
6+
<version>1.0.2</version>
77
<name>${project.artifactId}</name>
88
<description>基于HttpClient-4.4.1封装的工具类。支持插件式配置Header、插件式配置httpclient对象,这样就可以方便地自定义header信息、配置ssl、配置proxy、Cookie等</description>
99

src/main/java/com/arronlong/httpclientutil/common/HttpConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public HttpConfig map(Map<String, Object> map) {
202202
public HttpConfig json(String json) {
203203
this.json = json;
204204
Map<String, Object> map = new HashMap<String, Object>();
205-
map.put(Utils.ENTITY_STRING, json);
205+
map.put(Utils.ENTITY_JSON, json);
206206
maps.set(map);
207207
return this;
208208
}

src/main/java/com/arronlong/httpclientutil/common/Utils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ public class Utils {
3939

4040
//传入参数特定类型
4141
public static final String ENTITY_STRING="$ENTITY_STRING$";
42+
public static final String ENTITY_JSON="$ENTITY_JSON$";
4243
public static final String ENTITY_FILE="$ENTITY_FILEE$";
4344
public static final String ENTITY_BYTES="$ENTITY_BYTES$";
4445
public static final String ENTITY_INPUTSTREAM="$ENTITY_INPUTSTREAM$";
4546
public static final String ENTITY_SERIALIZABLE="$ENTITY_SERIALIZABLE$";
4647
public static final String ENTITY_MULTIPART="$ENTITY_MULTIPART$";
47-
private static final List<String> SPECIAL_ENTITIY = Arrays.asList(ENTITY_STRING, ENTITY_BYTES, ENTITY_FILE, ENTITY_INPUTSTREAM, ENTITY_SERIALIZABLE, ENTITY_MULTIPART);
48+
private static final List<String> SPECIAL_ENTITIY = Arrays.asList(ENTITY_STRING, ENTITY_JSON, ENTITY_BYTES, ENTITY_FILE, ENTITY_INPUTSTREAM, ENTITY_SERIALIZABLE, ENTITY_MULTIPART);
4849

4950
/**
5051
* 是否开启debug,
@@ -92,6 +93,14 @@ public static HttpEntity map2HttpEntity(List<NameValuePair> nvps, Map<String, Ob
9293
if(ENTITY_STRING.equals(entry.getKey())){//string
9394
entity = new StringEntity(String.valueOf(entry.getValue()), encoding);
9495
break;
96+
}else if(ENTITY_JSON.equals(entry.getKey())){//json
97+
entity = new StringEntity(String.valueOf(entry.getValue()), encoding);
98+
String contentType = "application/json";
99+
if (encoding != null) {
100+
contentType += ";charset=" + encoding;
101+
}
102+
((StringEntity) entity).setContentType(contentType);
103+
break;
95104
}else if(ENTITY_BYTES.equals(entry.getKey())){//file
96105
entity = new ByteArrayEntity((byte[])entry.getValue());
97106
break;

0 commit comments

Comments
 (0)