Spring Boot发送http请求

因为项目要调用第三方网站接口获取信息(类似python爬虫),所以在后端项目中需要前端axios一样去构建请求获取信息。下面用简单的获取IP地址为例。

java 复制代码
@Slf4j
public class IPTest {

    // IP地址查询
    public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";

    // 未知地址
    public static final String UNKNOWN = "未知地址";

    private String getRealAddressByIP(String ip) {
        String address = UNKNOWN;

        if (true) {
            try {
                String rspStr = sendGet(IP_URL, "ip=" + ip + "&json=true" ,"GBK");
                if (StrUtil.isEmpty(rspStr)) {
                    return UNKNOWN;
                }
                JSONObject obj = JSONObject.parseObject(rspStr);
                String addr = obj.getString("addr");
                return String.format("%s" , addr);
            } catch (Exception e) {
            }
        }
        return address;
    }


     private 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;DigExt");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (ConnectException e) {   
        } catch (SocketTimeoutException e) {
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
            }
        }
        return result.toString();
    }


    public static void main(String[] args) {
        String ipaddr = getRealAddressByIP(你的ip);
        String address = ipaddr.equals("未知地址") ? "位置获取失败" : ipaddr;
        log.info(address);
    }
}

有些网站会做反爬机制,所以在创建URL的时候,加上这句 connection.setRequestProperty("User-Agent","Mozilla/4.0 compatible; MSIE 6.0; Windows NT 5.1;DigExt");可以解决大部分问题

相关推荐
程序员岳焱4 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
隆里卡那唔4 小时前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
麦兜*5 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了5 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅5 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头5 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
天河归来5 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
~山有木兮5 小时前
LiteHub中间件之限流实现
网络·http·中间件
IT_10246 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
bobz9656 小时前
动态规划
后端