Skip to content

Commit

Permalink
⚡ perf(1.0.3): 优化http中content-type的设置
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan4261 committed Feb 22, 2021
1 parent f707d49 commit 183fc66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/main/java/weaver/micro/devkit/http/BasicQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static String buildUrl(Map<String, String> param, String uri) {

/**
* overload
* @return ?param1=xx&param2=xx
*/
public static String buildQuery(Map<String, String> param) {
if (param == null)
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/weaver/micro/devkit/http/CommonHttpAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
Expand All @@ -26,7 +27,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -45,7 +45,7 @@ protected String initialValue() {
}
};

public static String getThreadEncoding(){
public static String getThreadEncoding() {
return threadEncoding.get();
}

Expand All @@ -64,11 +64,12 @@ public static void clearThreadLocal() {
// 等待连接池给出可用连接超时 ms
.setConnectionRequestTimeout(5000)
// 与目标服务器建立tcp连接超时 ms
.setConnectTimeout(5000)
.setConnectTimeout(15000)
// 服务器响应超时 ms
.setSocketTimeout(30000)
.setSocketTimeout(15000)
// 设置是否允许重定向(默认为true)
.setRedirectsEnabled(true).build();
.setRedirectsEnabled(true)
.build();

final static Header[] DEFAULT_USER_AGENT = new Header[]{
new BasicHeader("User-Agent",
Expand All @@ -88,6 +89,8 @@ public static CloseableHttpClient BUILD_DEFAULT_CLIENT() {

/**
* 最普通的GET请求
* param参数会拼接到url上
* 一般不推荐设置请求头, 很多应用服务器不会读取其以及请求的实体部分
*
* @param client 发信端
* @param uri 资源定位
Expand Down Expand Up @@ -131,10 +134,6 @@ public static HttpResponse doPostURLEncode(HttpClient client,
Assert.notNull(client);
Assert.notNull(uri);

if (headers == null)
headers = new HashMap<String, String>(1, 1f);
headers.put("Content-Type", "application/x-www-form-urlencoded");

// method header
HttpPost method = buildMethodHeader(new HttpPost(), uri, headers);

Expand Down Expand Up @@ -165,15 +164,12 @@ public static HttpResponse doPostJson(HttpClient client,
Assert.notNull(client);
Assert.notNull(uri);

if (headers == null)
headers = new HashMap<String, String>(1, 1f);
headers.put("Content-Type", "application/json");

// method header
HttpPost method = buildMethodHeader(new HttpPost(), uri, headers);

// body
method.setEntity(new StringEntity(json, threadEncoding.get()));
method.setEntity(new StringEntity(json,
ContentType.create("application/json", threadEncoding.get())));

return client.execute(method);
}
Expand All @@ -196,15 +192,11 @@ public static HttpResponse doPostMultipart(HttpClient client,
Assert.notNull(client);
Assert.notNull(uri);

if (headers == null)
headers = new HashMap<String, String>(1, 1f);
headers.put("Content-Type", "multipart/form-data");

// method header
HttpPost method = buildMethodHeader(new HttpPost(), uri, headers);

// body
final MultipartEntity entity =
MultipartEntity entity =
new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
null,
Charset.forName(threadEncoding.get()));
Expand Down

0 comments on commit 183fc66

Please # to comment.