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());
    }
}
相关推荐
Knight_AL5 小时前
基于 Netty 实现的 WebSocket 服务端
网络·websocket·网络协议
wuyk5556 小时前
《WiFi 嵌入式物联网开发全套实战》| 第 31 章 休眠唤醒 WiFi 断连、时间同步、网络恢复机制
网络·物联网·php
沫璃染墨7 小时前
从零入门计算机网络系列(一):计算机网络初识——从网络发展史到TCP/IP协议》
linux·网络·网络协议·tcp/ip·计算机网络
王大傻09287 小时前
堆叠注入(Stacked Queries Injection)详解:原理、利用与防御
服务器·网络·数据库·web安全·网络安全
艾芯微科技7 小时前
钰泰 ETA5055V330DD1E|SOT-223 3.3V LDO,低压差、大电流线性稳压选型分享
网络·单片机·嵌入式硬件·集成测试·51单片机
bigcarp7 小时前
安卓模拟器MUMU+tcpdump抓包Socket协议流量
服务器·网络·tcpdump
是个西兰花7 小时前
TCP套接字编程实战:从服务器搭建到多进程处理
服务器·网络协议·tcp/ip
谢亮_vipxieliang8 小时前
Go net/http 标准库深入——构建生产级 HTTP 服务
http·golang·iphone
懂点Tech8 小时前
ctfshow MISC入门图片篇(信息附加):misc11解题思路
网络
charliejohn9 小时前
计算机考研 408 网络 Http协议综合题
网络