目录
准备工作
1、准备pom.xml
本次项目使用springboot,使用依赖如下:
<dependencies>
<!-- 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- springboot测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- html解析 -->
<dependency>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>2.3.2</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
2、准备http工具类,类名是HttpUtils(自己找位置放置,只要能用到就可以了)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* 通用http发送方法
*
* @author ruoyi
*/
public class HttpUtils
{
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
/**
* 向指定 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;
try
{
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
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(), contentType));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (Exception ex)
{
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
}
一、通过ip获取所在地址位置
方法一、使用http://whois.pconline.com.cn
代码:
String url = "http://whois.pconline.com.cn/ipJson.jsp";
String ip = "223.72.75.159";
String rspStr = HttpUtils.sendGet(url, "ip=" + ip + "&json=true", "GBK");
JSONObject obj = JSONObject.parseObject(rspStr);
String region = obj.getString("pro");
String city = obj.getString("city");
System.out.println("所在地址位置:" + region + " " + city);
结果:
所在地址位置: 北京市 北京市
方法二、使用https://ip.cn/api/index
代码:
String url = "https://ip.cn/api/index";
String ip = "223.72.75.159";
String rspStr = HttpUtils.sendGet(url, "ip=" + ip + "&type=1", "UTF-8");
JSONObject obj = JSONObject.parseObject(rspStr);
String address = (String) obj.get("address");
System.out.println("所在地址位置:" + address);
结果:
所在地址位置:中国 北京 北京市 移动
二、通过手机号获取归属地
代码:
String url = "https://www.ip138.com/mobile.asp";
String mobile = "184XXXX8507";
Document doc = Jsoup.connect(url + "?mobile=" + mobile + "&action=mobile").timeout(1000 * 60 * 30).get();
JXDocument jxDocument = new JXDocument(doc.getAllElements());
Element element = (Element)jxDocument.selOne("//table/tbody/tr[2]/td[2]/span");
System.out.println("手机号归属地:" + element.text());
结果:
手机号归属地:河南 洛阳市