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());
    }
}
相关推荐
moonless02221 天前
FastAPI框架,这一小篇就能搞懂精髓。
http·fastapi
白帽黑客沐瑶2 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
树码小子2 天前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
绿箭柠檬茶2 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
FPGA_Linuxer2 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 12 天前
传输层协议UDP
网络·网络协议·udp
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
喵手3 天前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络
徐子元竟然被占了!!3 天前
实验-基本ACL
网络
ftpeak3 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app