Browse Source

fix: 修复http发送post请求时,产生中文乱码的问题。

mjh 4 years ago
parent
commit
4fd875eb01

+ 8 - 1
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/HttpServiceImpl.java

@@ -21,14 +21,19 @@ import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
 import javax.servlet.http.HttpServletRequest;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 
+/**
+ * @author wind
+ */
 @Service
 public class HttpServiceImpl implements HttpService {
+    @Override
     public String sendPost(String uri, JSONObject requestBody, String token) throws Exception {
-        StringEntity requestEntity = new StringEntity(JSON.toJSONString(requestBody));
+        StringEntity requestEntity = new StringEntity(JSON.toJSONString(requestBody), StandardCharsets.UTF_8);
         HttpPost httpPost = new HttpPost(uri);
         httpPost.setHeader("Authorization", token);
         httpPost.setHeader("Content-Type", "application/json;chartset=UTF-8");
@@ -42,6 +47,7 @@ public class HttpServiceImpl implements HttpService {
         return result;
     }
 
+    @Override
     public String sendGet(String uri, List<NameValuePair> params, String token) throws Exception {
         String completedUri;
         if (params != null && !params.isEmpty()) {
@@ -63,6 +69,7 @@ public class HttpServiceImpl implements HttpService {
         return result;
     }
 
+    @Override
     public String getToken() {
         String token;
         HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();