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");可以解决大部分问题

相关推荐
秋野酱1 小时前
基于javaweb的SpringBoot自习室预约系统设计与实现(源码+文档+部署讲解)
java·spring boot·后端
呆萌很1 小时前
基于 Spring Boot 瑞吉外卖系统开发(十五)
spring boot
weloveut1 小时前
西门子WinCC Unified PC的GraphQL使用手册
后端·python·graphql
会飞的架狗师2 小时前
【SpringBoot实战指南】集成Easy ES
spring boot·elasticsearch
老李不敲代码2 小时前
榕壹云打车系统:基于Spring Boot+MySQL+UniApp的开源网约车解决方案
spring boot·mysql·微信小程序·uni-app·软件需求
蒂法就是我3 小时前
详细说说Spring的IOC机制
java·后端·spring
秋野酱3 小时前
基于javaweb的SpringBoot高校图书馆座位预约系统设计与实现(源码+文档+部署讲解)
java·spring boot·后端
HWL56794 小时前
Express项目解决跨域问题
前端·后端·中间件·node.js·express
-曾牛4 小时前
Spring AI 集成 Mistral AI:构建高效多语言对话助手的实战指南
java·人工智能·后端·spring·microsoft·spring ai
小白自救计划5 小时前
网络协议分析 实验七 FTP、HTTP、DHCP
网络·网络协议·http