当前位置: 首页 > article >正文

Spring Boot发送http请求

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

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


http://www.kler.cn/news/284961.html

相关文章:

  • C语言补习课
  • gdb 教程
  • springboot学习(2)
  • 美团2024秋招编程题:小美的red子序列数量之和
  • WebSocket 实现消息推送
  • AOP 面向切片编程
  • 我的推荐:腾讯云罗云《从零构建向量数据库》
  • 无人机之遥控器防水性能篇
  • Ubuntu 20.04 安装 GitHub CLI(gh),并使用
  • C语言——简单的do while循环找100~999之间的水仙花数(所有的三位水仙花数)
  • 数据结构(三)——双向链表,循环链表,内核链表,栈和队列
  • 『功能项目』怪物反击主角复活【14】
  • spring security 会话管理
  • 苹果M4芯片Mac全面曝光 或10月发布
  • OpenHarmony轻量设备Hi3861芯片开发板启动流程分析
  • redis能正常访问,但是springboot编译报错
  • 【Go函数详解】二、参数传递、变长参数与多返回值
  • java定时服务
  • Python学习日志(1)——安装
  • Linux-arm64中断现场保护详解
  • MySQL 集群技术全攻略:从搭建到优化(上)
  • 分类模型评估指标——准确率、精准率、召回率、F1、ROC曲线、AUC曲线
  • 快递盒检测检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • RAG 向量数据库:掌握 Elasticsearch 作为向量数据库的终极指南
  • 【Python零基础】文件使用和异常处理
  • Vue(四) 组件、单文件组件、非单文件组件,重要的内置关系
  • 【计组 | Cache原理】讲透Cache的所有概念与题型方法
  • 大模型好书案例——《BERT基础教程:Transformer大模型实战》(附PDF)
  • LuaJit分析(一)LuaJit交叉编译
  • TCP的连接与断开