转载自:https://www.novelweb.cn/archives/84
HTTP请求工具类
需要引入lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
package com.tool.http;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;
/**
* @description:HTTP请求类
* @author: Dai Yuanchuan
* @create: 2018-12-12 15:14
**/
@Slf4j
public class HttpUtils {
/**
* 支持的Http method
*/
private static enum HttpMethod {
/**
* HTTP 请求方式
*/
POST,
DELETE,
GET,
PUT,
HEAD
}
private static String structureHttpURLConnection(String url, HttpMethod method, String encoding,
Map params, Map<String, String> headers, StringBuilder paramsStr,
int connectTimeout, int readTimeout) throws IOException {
URL uUrl;
HttpURLConnection conn = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
//创建和初始化连接
uUrl = new URL(url);
conn = (HttpURLConnection) uUrl.openConnection();
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
conn.setRequestMethod(method.toString());
conn.setDoOutput(true);
conn.setDoInput(true);
//设置连接超时时间
conn.setConnectTimeout(connectTimeout);
//设置读取超时时间
conn.setReadTimeout(readTimeout);
//指定请求header参数
if (headers != null && headers.size() > 0) {
Set<String> headerSet = headers.keySet();
for (String key : headerSet) {
conn.setRequestProperty(key, headers.get(key));
}
}
if (paramsStr != null && method == HttpMethod.POST) {
//发送请求参数
out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), encoding));
out.write(paramsStr.toString());
out.flush();
}
//接收返回结果
StringBuilder result = new StringBuilder();
log.info(result.toString());
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
if (in != null) {
String line = "";
while ((line = in.readLine()) != null) {
result.append(line);
}
}
return result.toString();
} catch (Exception e) {
log.error("调用接口[" + url + "]失败!请求URL:" + url + ",参数:" + params, e.getMessage());
//处理错误流,提高http连接被重用的几率
try {
byte[] buf = new byte[100];
InputStream es = conn.getErrorStream();
if (es != null) {
while (es.read(buf) > 0) {
;
}
es.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
close(conn, out, in);
}
return null;
}
private static void close(HttpURLConnection conn, BufferedWriter out, BufferedReader in) {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
//关闭连接
if (conn != null) {
conn.disconnect();
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static String invokeUrl(
String url, Map params, Map<String, String> headers,
int connectTimeout, int readTimeout,
String encoding, HttpMethod method) {
//构造请求参数字符串
StringBuilder paramsStr = null;
if (params != null) {
paramsStr = new StringBuilder();
Set<Map.Entry> entries = params.entrySet();
for (Map.Entry entry : entries) {
String value = (entry.getValue() != null) ?
(String.valueOf(entry.getValue())) : "";
paramsStr.append(entry.getKey() + "=" + value + "&");
}
//只有POST方法才能通过OutputStream(即form的形式)提交参数
if (method != HttpMethod.POST) {
url += "?" + paramsStr.toString();
}
}
try {
return structureHttpURLConnection(url, method, encoding, params, headers, paramsStr, connectTimeout, readTimeout);
} catch (IOException e) {
log.error(e.getMessage());
}
return null;
}
/**
* POST方法提交Http请求,语义为“增加” <br/>
* 注意:Http方法中只有POST方法才能使用body来提交内容
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String post(String url, Map params, int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, null, connectTimeout, readTimeout, charset, HttpMethod.POST);
}
/**
* POST方法提交Http请求,语义为“增加” <br/>
* 注意:Http方法中只有POST方法才能使用body来提交内容
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param headers 请求头参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String post(String url, Map params,
Map<String, String> headers,
int connectTimeout, int readTimeout,
String charset) {
return invokeUrl(url, params, headers, connectTimeout, readTimeout, charset, HttpMethod.POST);
}
/**
* GET方法提交Http请求,语义为“查询”
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String get(String url, Map params, int connectTimeout,
int readTimeout, String charset) {
return invokeUrl(url, params, null, connectTimeout,
readTimeout, charset, HttpMethod.GET);
}
/**
* GET方法提交Http请求,语义为“查询”
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param headers 请求头参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String get(String url, Map params,
Map<String, String> headers,
int connectTimeout,
int readTimeout, String charset) {
return invokeUrl(url, params, headers, connectTimeout,
readTimeout, charset, HttpMethod.GET);
}
/**
* PUT方法提交Http请求,语义为“更改” <br/>
* 注意:PUT方法也是使用url提交参数内容而非body,所以参数最大长度收到服务器端实现的限制,Resin大概是8K
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String put(String url, Map params,
int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, null,
connectTimeout, readTimeout, charset, HttpMethod.PUT);
}
/**
* PUT方法提交Http请求,语义为“更改” <br/>
* 注意:PUT方法也是使用url提交参数内容而非body,所以参数最大长度收到服务器端实现的限制,Resin大概是8K
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param headers 请求头参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String put(String url, Map params, Map<String, String> headers,
int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, headers, connectTimeout,
readTimeout, charset, HttpMethod.PUT);
}
/**
* DELETE方法提交Http请求,语义为“删除”
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String delete(String url, Map params,
int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, null,
connectTimeout, readTimeout, charset, HttpMethod.DELETE);
}
/**
* DELETE方法提交Http请求,语义为“删除”
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param headers 请求头参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String delete(String url, Map params,
Map<String, String> headers,
int connectTimeout,
int readTimeout, String charset) {
return invokeUrl(url, params, headers, connectTimeout,
readTimeout, charset, HttpMethod.DELETE);
}
/**
* HEAD方法提交Http请求,语义同GET方法 <br/>
* 跟GET方法不同的是,用该方法请求,服务端不返回message body只返回头信息,能节省带宽
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String head(String url, Map params, int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, null, connectTimeout, readTimeout, charset, HttpMethod.HEAD);
}
/**
* HEAD方法提交Http请求,语义同GET方法 <br/>
* 跟GET方法不同的是,用该方法请求,服务端不返回message body只返回头信息,能节省带宽
*
* @param url 资源路径(如果url中已经包含参数,则params应该为null)
* @param params 参数
* @param headers 请求头参数
* @param connectTimeout 连接超时时间(单位为ms)
* @param readTimeout 读取超时时间(单位为ms)
* @param charset 字符集(一般该为“utf-8”)
* @return
*/
public static String head(String url, Map params, Map<String, String> headers, int connectTimeout, int readTimeout, String charset) {
return invokeUrl(url, params, headers, connectTimeout, readTimeout, charset, HttpMethod.HEAD);
}
public static void main(String[] args) {
// 构建参数
Map params = new HashMap(16);
params.put("phoneNo", "中文");
// 使用get请求
String str = HttpUtils.get("http://localhost:9092/elis_smp_als_dmz/do/app/activitySupport/demo", params, 3000, 3000, "UTF-8");
System.out.println(str);
}
}
IP地址工具类
需要引入fastjson、lombok、commons
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
package com.tool.http;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 获取访问IP地址等信息
* @author: Dai Yuanchuan
* @create: 2019-03-29 11:16
**/
@Slf4j
public class IpUtils {
/**
* 常量 定义 unknown
*/
public static final String UNKNOWN = "unknown";
/**
* 常量 定义本地IP ipv4 形式
*/
public static final String LOCALHOST_IPV4 = "127.0.0.1";
/**
* 常量 定义本地IP ipv6 形式
*/
public static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
/**
* 常量 获取IP地址方法中的IP长度
*/
public static final int IP_LENGTH = 15;
/**
* 常量 逗号
*/
public static final String COMMA = ",";
/**
* 常量 请求失败时 返回的状态码
*/
public static final String FAIL = "1";
/**
* 常量 请求成功时 返回的状态码
*/
public static final String SUCCESS = "0";
/**
* 获取IP地址
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个
* 而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串
* 则为真实IP地址
*
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request) {
try {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (ip.equals(LOCALHOST_IPV4)) {
//根据网卡取本机配置的IP
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
ip = inetAddress.getHostAddress();
}
}
// 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ip != null && ip.length() > IP_LENGTH) {
if (ip.indexOf(COMMA) > 0) {
ip = ip.substring(0, ip.indexOf(COMMA));
}
}
return ip;
} catch (Exception e) {
log.error("获取IP地址时出现异常");
log.error("" + e);
return "-1";
}
}
/**
* 通过这个地址获取IP实际地理位置信息
*/
public static String getRealAddressByIP(String ip) {
// 这个地址是淘宝的
String taoBaoUrl = "http://ip.taobao.com/service/getIpInfo.php";
// 内网不查询
if (IpUtils.internalIp(ip)) {
return "内网IP";
}
Map params = new HashMap(1);
params.put("ip", ip);
String rspStr;
try{
rspStr = HttpUtils.post(taoBaoUrl, params, 2000, 2000, "UTF-8");
}catch (Exception e){
return getAddressToPconLine(ip);
}
if (StringUtils.isBlank(rspStr)) {
return getAddressToPconLine(ip);
}
JSONObject obj = JSONObject.parseObject(rspStr);
JSONObject data = obj.getObject("data", JSONObject.class);
String isp = data.getString("isp");
String region = data.getString("region");
String city = data.getString("city");
return region + " " + city + " " + isp;
}
/**
* 获取太平洋网的IP地址查看
*
* @param ip
* @return
*/
private static String getAddressToPconLine(String ip) {
// 这个地址是太平洋网的
String taiPingYang = "http://whois.pconline.com.cn/ip.jsp";
// 未能查询到时返回
String address = "XX XX";
Map params = new HashMap(1);
params.put("ip", ip);
String rspStr = HttpUtils.post(taiPingYang, params, 3000, 3000, "GBK");
if (StringUtils.isEmpty(rspStr)) {
return address;
}
return rspStr;
}
/**
* 判断IP地址是否为内网IP
*
* @param ip
* @return true:内网IP地址 false:不是内网IP
*/
private static boolean internalIp(String ip) {
// 定义结果集
boolean localhostIpv4 = ip.equals(LOCALHOST_IPV4);
boolean localhostIpv6 = ip.equals(LOCALHOST_IPV6);
if (localhostIpv4 || localhostIpv6) {
return true;
} else {
byte[] address = textToNumericFormatV4(ip);
return internalIp(address);
}
}
private static boolean internalIp(byte[] address) {
final byte b0 = address[0];
final byte b1 = address[1];
// 10.x.x.x/8
final byte section1 = 0x0A;
// 172.16.x.x/12
final byte section2 = (byte) 0xAC;
final byte section3 = (byte) 0x10;
final byte section4 = (byte) 0x1F;
// 192.168.x.x/16
final byte section5 = (byte) 0xC0;
final byte section6 = (byte) 0xA8;
switch (b0) {
case section1:
return true;
case section2:
if (b1 >= section3 && b1 <= section4) {
return true;
}
case section5:
switch (b1) {
case section6:
return true;
default:
return false;
}
default:
return false;
}
}
/**
* 将IPv4地址转换成字节
*
* @param text IPv4地址
* @return byte 字节
*/
private static byte[] textToNumericFormatV4(String text) {
if (text.length() == 0) {
return null;
}
byte[] bytes = new byte[4];
String[] elements = text.split("\\.", -1);
try {
long l;
int i;
switch (elements.length) {
case 1:
l = Long.parseLong(elements[0]);
long long4294967295L = 4294967295L;
if ((l < 0L) || (l > long4294967295L)) {
return null;
}
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 2:
long long255L = 255L;
l = Integer.parseInt(elements[0]);
if ((l < 0L) || (l > long255L)) {
return null;
}
bytes[0] = (byte) (int) (l & 0xFF);
l = Integer.parseInt(elements[1]);
long long16777215L = 16777215L;
if ((l < 0L) || (l > long16777215L)) {
return null;
}
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 3:
int d = 2;
for (i = 0; i < d; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
l = Integer.parseInt(elements[2]);
long c = 65535L;
if ((l < 0L) || (l > c)) {
return null;
}
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 4:
int max = 4;
for (i = 0; i < max; ++i) {
l = Integer.parseInt(elements[i]);
if (loop(l, elements, i).equals(FAIL)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
break;
default:
return null;
}
} catch (NumberFormatException e) {
return null;
}
return bytes;
}
private static String loop(long l, String[] elements, int i) {
l = Integer.parseInt(elements[i]);
long max = 255L;
if ((l < 0L) || (l > max)) {
return FAIL;
}
return SUCCESS;
}
}