通用http发送方法增加参数 contentType 编码类型

dep_wangsr
RuoYi 5 years ago committed by Limy
parent 147a85ba36
commit 03f8adcbde

@ -12,6 +12,11 @@ public class Constants
*/
public static final String UTF8 = "UTF-8";
/**
* GBK
*/
public static final String GBK = "GBK";
/**
*
*/
@ -31,7 +36,7 @@ public class Constants
*
*/
public static final String LOGOUT = "Logout";
/**
*
*/

@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.http.HttpUtils;
/**
@ -33,7 +34,7 @@ public class AddressUtils
{
try
{
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true");
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
if (StringUtils.isEmpty(rspStr))
{
log.error("获取地理位置异常 {}", ip);
@ -46,7 +47,7 @@ public class AddressUtils
}
catch (Exception e)
{
log.error("获取地理位置异常 {}", ip);
log.error("获取地理位置异常 {}", e);
}
}
return address;

@ -18,6 +18,7 @@ import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
/**
* http
@ -36,6 +37,19 @@ public class HttpUtils
* @return
*/
public static String sendGet(String url, String param)
{
return sendGet(url, param, Constants.UTF8);
}
/**
* URL GET
*
* @param url URL
* @param param name1=value1&name2=value2
* @param contentType
* @return
*/
public static String sendGet(String url, String param, String contentType)
{
StringBuilder result = new StringBuilder();
BufferedReader in = null;
@ -49,7 +63,7 @@ public class HttpUtils
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
String line;
while ((line = in.readLine()) != null)
{

Loading…
Cancel
Save