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
| @@ -13,7 +13,12 @@ public class Constants | @@ -13,7 +13,12 @@ public class Constants | ||
| 13 | * UTF-8 字符集 | 13 | * UTF-8 字符集 |
| 14 | */ | 14 | */ |
| 15 | public static final String UTF8 = "UTF-8"; | 15 | public static final String UTF8 = "UTF-8"; |
| 16 | - | 16 | + |
| 17 | + /** | ||
| 18 | + * GBK 字符集 | ||
| 19 | + */ | ||
| 20 | + public static final String GBK = "GBK"; | ||
| 21 | + | ||
| 17 | /** | 22 | /** |
| 18 | * 通用成功标识 | 23 | * 通用成功标识 |
| 19 | */ | 24 | */ |
ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
| @@ -18,6 +18,7 @@ import javax.net.ssl.TrustManager; | @@ -18,6 +18,7 @@ import javax.net.ssl.TrustManager; | ||
| 18 | import javax.net.ssl.X509TrustManager; | 18 | import javax.net.ssl.X509TrustManager; |
| 19 | import org.slf4j.Logger; | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; | 20 | import org.slf4j.LoggerFactory; |
| 21 | +import com.ruoyi.common.constant.Constants; | ||
| 21 | 22 | ||
| 22 | /** | 23 | /** |
| 23 | * 通用http发送方法 | 24 | * 通用http发送方法 |
| @@ -37,6 +38,19 @@ public class HttpUtils | @@ -37,6 +38,19 @@ public class HttpUtils | ||
| 37 | */ | 38 | */ |
| 38 | public static String sendGet(String url, String param) | 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 | StringBuilder result = new StringBuilder(); | 54 | StringBuilder result = new StringBuilder(); |
| 41 | BufferedReader in = null; | 55 | BufferedReader in = null; |
| 42 | try | 56 | try |
| @@ -49,7 +63,7 @@ public class HttpUtils | @@ -49,7 +63,7 @@ public class HttpUtils | ||
| 49 | connection.setRequestProperty("connection", "Keep-Alive"); | 63 | connection.setRequestProperty("connection", "Keep-Alive"); |
| 50 | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | 64 | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| 51 | connection.connect(); | 65 | connection.connect(); |
| 52 | - in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); | 66 | + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType)); |
| 53 | String line; | 67 | String line; |
| 54 | while ((line = in.readLine()) != null) | 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,6 +3,7 @@ package com.ruoyi.common.utils.ip; | ||
| 3 | import org.slf4j.Logger; | 3 | import org.slf4j.Logger; |
| 4 | import org.slf4j.LoggerFactory; | 4 | import org.slf4j.LoggerFactory; |
| 5 | import com.alibaba.fastjson.JSONObject; | 5 | import com.alibaba.fastjson.JSONObject; |
| 6 | +import com.ruoyi.common.constant.Constants; | ||
| 6 | import com.ruoyi.common.utils.StringUtils; | 7 | import com.ruoyi.common.utils.StringUtils; |
| 7 | import com.ruoyi.common.utils.http.HttpUtils; | 8 | import com.ruoyi.common.utils.http.HttpUtils; |
| 8 | import com.ruoyi.framework.config.RuoYiConfig; | 9 | import com.ruoyi.framework.config.RuoYiConfig; |
| @@ -34,7 +35,7 @@ public class AddressUtils | @@ -34,7 +35,7 @@ public class AddressUtils | ||
| 34 | { | 35 | { |
| 35 | try | 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 | if (StringUtils.isEmpty(rspStr)) | 39 | if (StringUtils.isEmpty(rspStr)) |
| 39 | { | 40 | { |
| 40 | log.error("获取地理位置异常 {}", ip); | 41 | log.error("获取地理位置异常 {}", ip); |