cn.hutool.http.HttpUtil类get方法不支持获取重定向地址(避坑)

问题

工作中场景需要通过获取url地址内容,展示返回给客户端,但上线后发现不满足需求,原因是url地址进行302重定向,

进一步了解是因为HttpUtil.get方法不能获取重定向地址,需要使用HttpUtil.createGet()来设置打开重定;

理解302:

302 表示临时性重定向,访问一个Url时,被重定向到另一个url上;常用于页面跳转。

302与301的区别:

301是指永久性的移动,302是暂时性的,即以后还可能有变化;

其它重定向方式:

在响应头中加入Location参数。浏览器接受到带有location头的响应时,就会跳转到相应的地址。

Spring实现302的几种方式:
1、使用RedirectView实现重定向
复制代码
    @GetMapping("/redirect/v1")
    public RedirectView redirectV1() {
        //创建RedirectView对象并设置目标URL
        RedirectView view = new RedirectView();
        //view.setUrl("https://www.baidu.com");
        view.setUrl("/springboot/redirect/index");
        Properties properties = new Properties();
        properties.setProperty("name", "make");
        view.setAttributes(properties);
        return view;
    }
2、HttpServletResponse重定向

通过HttpServletResponse往输出流中写数据的方式,来返回结果, 实现重定向

复制代码
    @ResponseBody
    @GetMapping("/redirect/v2")
    public void redirectV2(HttpServletResponse response) throws IOException {
        response.sendRedirect("https://www.sina.com.cn");
    }
3、通过redirect关键词

常适用于返回视图的接口,在返回的字符串前面添加redirect:方式来告诉Spring框架,需要做302重定向处理;

复制代码
    @ResponseBody
    @GetMapping("/redirect/v3")
    public String redirectV3() throws IOException {
         return "redirect:/redirect/index?base=r1";
    }

完整测试:

复制代码
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Controller
public class RedirectController {


    /**
     * 使用RedirectView实现重定向
     */
    @GetMapping("/redirect/v1")
    public RedirectView redirectV1() {
        //创建RedirectView对象并设置目标URL
        RedirectView view = new RedirectView();
        //view.setUrl("https://www.baidu.com");
        view.setUrl("/springboot/redirect/index");
        Properties properties = new Properties();
        properties.setProperty("name", "make");
        view.setAttributes(properties);
        return view;
    }

    /**
     * HttpServletResponse重定向
     * 通过HttpServletResponse往输出流中写数据的方式,来返回结果, 实现重定向
     */
    @ResponseBody
    @GetMapping("/redirect/v2")
    public void redirectV2(HttpServletResponse response) throws IOException {
        response.sendRedirect("https://www.sina.com.cn");
    }

    /**
     *  返回redirect
     *  常适用于返回视图的接口,在返回的字符串前面添加redirect:方式来告诉Spring框架,需要做302重定向处理;
     */
    @ResponseBody
    @GetMapping("/redirect/v3")
    public String redirectV3() throws IOException {
         return "redirect:/redirect/index?base=r1";
    }


    @ResponseBody
    @GetMapping(path = "/redirect/index")
    public String index(HttpServletRequest request) {
        return "重定向访问! " + JSON.toJSONString(request.getParameterMap());
    }
}
相关推荐
楷哥爱开发8 小时前
如何使用 Claude Fable 5 进行网页抓取?2026最新实战教程
大数据·网络·人工智能
Sirius Wu8 小时前
OpenClaw Skill:Matplotlib 可视化技能 + 沙箱双层隔离完整详解
服务器·网络·人工智能·安全·ai·架构·aigc
测试员周周8 小时前
【skills5】一份 spec 生成 k6/JMeter/Locust:性能压测 + 全站截图,上线前的双保险
功能测试·网络协议·测试工具·jmeter·http·自动化·集成测试
酣大智9 小时前
常用 IP 协议号大全(IP 层承载的上层协议)
网络·网络协议·tcp/ip
大公产经晚间消息11 小时前
《寻访独角兽》首期走进太仓,探访小科智行的硬科技“突围”
网络·人工智能·科技
JoyCong199813 小时前
ToDesk新功能科普:屏幕墙、协作模式、设备别名、USB映射...
网络·科技·电脑·远程工作·远程操作
数智化管理手记13 小时前
智能财务如何减少财务加班?智能财务落地需要哪些工具支撑?
大数据·网络·数据库·数据挖掘·精益工程
paopaokaka_luck16 小时前
基于Springboot3+vue3的旅游景区点评系统(AI审核、webSocket聊天、协同过滤算法、Echarts图形化分析)
websocket·网络协议·旅游
其实防守也摸鱼17 小时前
运维--安全与数据库
网络·数据库·学习·安全·安全威胁分析·数据库架构·软件安全
wbs_scy18 小时前
仿 muduo 高并发服务器项目:实现 Connection 连接管理模块,并整合 Any 上下文
运维·服务器·网络