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());
    }
}
相关推荐
jenchoi41336 分钟前
【2025-11-23】软件供应链安全日报:最新漏洞预警与投毒预警情报汇总
网络·数据库·安全·web安全·网络安全
独行soc2 小时前
2025年渗透测试面试题总结-258(题目+回答)
网络·python·安全·web安全·渗透测试·安全狮
AI绘画小333 小时前
网络安全(黑客技术)—2025自学手册
网络·安全·web安全·网络安全·渗透测试
国服第二切图仔3 小时前
Electron for 鸿蒙PC实战案例Gitcode口袋工具之HTTP请求封装的技术实现与设计解析
http·electron·gitcode
s09071364 小时前
ZYNQ DMA to UDP 数据传输系统设计文档
网络协议·fpga开发·udp
恒创科技HK5 小时前
香港服务器流量有限制和带宽有限制区别在哪?
运维·服务器·网络
hazy1k6 小时前
ESP32基础-Socket通信 (TCP/UDP)
c语言·单片机·嵌入式硬件·网络协议·tcp/ip·udp·esp32
赖small强6 小时前
【Linux 网络基础】WebSockets 技术指南
linux·网络·https·websockets·ping/pong
专家大圣6 小时前
告别局域网束缚!飞牛云 NAS+cpolar 让远程管理更简单
开发语言·网络·内网穿透·cpolar
xinxinhenmeihao7 小时前
爬虫为什么要用动态ip?动态IP在爬虫中起到哪些作用?
爬虫·网络协议·tcp/ip