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

相关推荐
小北方城市网3 小时前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf
毕设源码-钟学长5 小时前
【开题答辩全过程】以 基于SpringBoot的智能书城推荐系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
青春男大6 小时前
Redis和RedisTemplate快速上手
java·数据库·redis·后端·spring·缓存
DARLING Zero two♡6 小时前
【计算机网络】简学深悟启示录:http
网络协议·计算机网络·http
张张努力变强7 小时前
C++ 类和对象(四):const成员函数、取地址运算符重载全精讲
开发语言·数据结构·c++·后端
Yu_Lijing7 小时前
《图解HTTP》笔记与读后感(上)
网络·笔记·网络协议·http
不吃香菜学java8 小时前
springboot左脚踩右脚螺旋升天系列-整合开发
java·spring boot·后端·spring·ssm
奋进的芋圆10 小时前
Java 锁事详解
java·spring boot·后端
qq_124987075310 小时前
基于springboot的河南特色美食分享系统的设计与实现(源码+论文+部署+安装)
java·大数据·人工智能·spring boot·计算机毕设·计算机毕业设计
郑州光合科技余经理10 小时前
技术架构:海外版外卖平台搭建全攻略
java·大数据·人工智能·后端·小程序·架构·php