gpt4 book ai didi

java - 重定向使用和 POST 参数

转载 作者:行者123 更新时间:2023-11-30 05:35:59 24 4
gpt4 key购买 nike

我想用 Spring 实现这个例子:

@PostMapping(value = "/redirect/to_payment/{token}")
public ModelAndView handleRedirectMessage(@PathVariable("token") String token,
@RequestBody Transaction transaction, HttpServletRequest request) throws Exception {

String url = "http://www.someserver.com";
String post_token = "1234561234543322";

// Open here the link and redirect the

return new ModelAndView("redirect:" + url);
}

如何打开此链接,将 post_token 作为 POST 参数发送并将打开的页面返回给用户?

有什么方法可以为用户实现这个解决方案吗?作为第二种解决方案,我可以将此页面返回给用户并将 post_token 作为参数吗?

最佳答案

您可以使用okhttp3依赖,从服务器发送http请求,然后将okhttp对象的响应体返回给客户端。

这是一个例子:

@PostMapping(value = "/redirect/to_payment/{token}")
public ModelAndView handleRedirectMessage(@PathVariable("token") String token,
@RequestBody Transaction transaction, HttpServletRequest request) throws Exception {

String url = "http://www.someserver.com";
String post_token = "1234561234543322";

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(url)
.post(null) // because u have no body
.addHeader("Authorization", post_token)
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();

return new ModelAndView(response.body().toString()); // or something like this
}

当然你必须处理IOException并且最后的body方法可能有点不同。

提示:您可以使用 postman 通过模拟您的请求轻松生成 OkHttp 或 Unirest 请求代码。

关于java - 重定向使用和 POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com