Commit 4f1f0bdbd306e06c88e99408fb9a5a456f38162c
1 parent
f3d0bf6c
通用http发送方法增加参数 contentType 编码类型
Showing
3 changed files
with
23 additions
and
3 deletions
ruoyi/src/main/java/com/ruoyi/common/constant/Constants.java
ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
| ... | ... | @@ -18,6 +18,7 @@ import javax.net.ssl.TrustManager; |
| 18 | 18 | import javax.net.ssl.X509TrustManager; |
| 19 | 19 | import org.slf4j.Logger; |
| 20 | 20 | import org.slf4j.LoggerFactory; |
| 21 | +import com.ruoyi.common.constant.Constants; | |
| 21 | 22 | |
| 22 | 23 | /** |
| 23 | 24 | * 通用http发送方法 |
| ... | ... | @@ -37,6 +38,19 @@ public class HttpUtils |
| 37 | 38 | */ |
| 38 | 39 | public static String sendGet(String url, String param) |
| 39 | 40 | { |
| 41 | + return sendGet(url, param, Constants.UTF8); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * 向指定 URL 发送GET方法的请求 | |
| 46 | + * | |
| 47 | + * @param url 发送请求的 URL | |
| 48 | + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 | |
| 49 | + * @param contentType 编码类型 | |
| 50 | + * @return 所代表远程资源的响应结果 | |
| 51 | + */ | |
| 52 | + public static String sendGet(String url, String param, String contentType) | |
| 53 | + { | |
| 40 | 54 | StringBuilder result = new StringBuilder(); |
| 41 | 55 | BufferedReader in = null; |
| 42 | 56 | try |
| ... | ... | @@ -49,7 +63,7 @@ public class HttpUtils |
| 49 | 63 | connection.setRequestProperty("connection", "Keep-Alive"); |
| 50 | 64 | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| 51 | 65 | connection.connect(); |
| 52 | - in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); | |
| 66 | + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType)); | |
| 53 | 67 | String line; |
| 54 | 68 | while ((line = in.readLine()) != null) |
| 55 | 69 | { | ... | ... |
ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java
| ... | ... | @@ -3,6 +3,7 @@ package com.ruoyi.common.utils.ip; |
| 3 | 3 | import org.slf4j.Logger; |
| 4 | 4 | import org.slf4j.LoggerFactory; |
| 5 | 5 | import com.alibaba.fastjson.JSONObject; |
| 6 | +import com.ruoyi.common.constant.Constants; | |
| 6 | 7 | import com.ruoyi.common.utils.StringUtils; |
| 7 | 8 | import com.ruoyi.common.utils.http.HttpUtils; |
| 8 | 9 | import com.ruoyi.framework.config.RuoYiConfig; |
| ... | ... | @@ -34,7 +35,7 @@ public class AddressUtils |
| 34 | 35 | { |
| 35 | 36 | try |
| 36 | 37 | { |
| 37 | - String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true"); | |
| 38 | + String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK); | |
| 38 | 39 | if (StringUtils.isEmpty(rspStr)) |
| 39 | 40 | { |
| 40 | 41 | log.error("获取地理位置异常 {}", ip); | ... | ... |