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

本文详细介绍了在SpringBoot应用中遇到302重定向问题的解决方案,包括使用RedirectView、HttpServletResponse和redirect关键词实现重定向,以及302与301的区别和浏览器行为。
摘要由CSDN通过智能技术生成
问题

工作中场景需要通过获取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());
    }
}
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hutool中的HttpUtil.post方法是用来发送POST请求的工具方法。它通过HttpRequest对象的封装来实现灵活的Http请求操作。可以使用该方法来发送POST请求,并指定请求的URL和请求参数。具体的方法签名如下: ```java public static String post(String urlString, Map<String, Object> paramMap) ``` 该方法使用Map对象来传递请求参数,然后通过链式调用的方式设置请求的超时时间、执行请求并获取服务器的响应内容。例如,可以使用以下代码来调用该方法: ```java String urlString = "http://example.com/api"; Map<String, Object> paramMap = new HashMap<>(); paramMap.put("param1", "value1"); paramMap.put("param2", "value2"); String response = HttpUtil.post(urlString, paramMap); ``` 在上述代码中,我们指定了请求的URL为"http://example.com/api",并设置了两个请求参数"param1"和"param2"的值为"value1"和"value2"。最后调用HttpUtil.post方法执行POST请求,并将服务器的响应内容赋值给response变量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [hutool Http 工具发送POST请求的几种方式](https://blog.csdn.net/lly576403061/article/details/131181907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值