gpt4 book ai didi

java - 使用 Webflux 实现 POST

转载 作者:行者123 更新时间:2023-12-02 09:30:08 25 4
gpt4 key购买 nike

我有这个 html 表单,当我在网络浏览器中打开它时,它工作正常:

<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8" />
<meta HTTP-EQUIV="Cache-Control" CONTENT="no cache" />
<meta HTTP-EQUIV="Pragma" CONTENT="no cache" />
<meta HTTP-EQUIV="Expires" CONTENT="0" />
</head>
<body OnLoad="AutoSubmitForm();">
<form name="downloadForm" action="https://c3-test.wirecard.com/acssim/app/bank" method="POST">
<input type="hidden" name="PaReq" value="eJxtU9tuozAQ......." />
<input type="hidden" name="TermUrl" value="https://www.test.com" />
<input type="hidden" name="MD" value="optionalValue" />
<SCRIPT LANGUAGE="Javascript">
function AutoSubmitForm() { document.downloadForm.submit();}
</SCRIPT>
<input type="submit" name="continue" value="Continue" />
</form>
</body>
</html>

我尝试了这段代码:

WebClient.Builder builder = WebClient.builder();
WebClient cliensst = builder.build();
String w = cliensst.post().uri(url)
.header("PaReq", pareq)
.header("TermUrl", "https://www.test.com")
.header("MD", "")
.accept(MediaType.TEXT_PLAIN)
.contentType(MediaType.TEXT_PLAIN)
.retrieve()
.bodyToMono(String.class).block();

但我收到错误:org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest:400错误请求

实现这个的正确方法是什么?

最佳答案

你可以试试这个。

public static <T> T executeApiForFormData(String url, Map<String, String> payload, Map<String, String> headers, Class<T> clazz) {
WebClient client;
WebClient.Builder builder = WebClient.builder().baseUrl(url);

MultiValueMap<String, String> formData = new HttpHeaders();
formData.setAll(payload);

if (!ObjectUtils.isEmpty(headers)) {
MultiValueMap<String, String> map = new HttpHeaders();
map.setAll(headers);
builder.defaultHeaders((existingHeaders) -> existingHeaders.addAll(map));
}


client = builder.build();

return client
.method(HttpMethod.POST)
.body(BodyInserters.fromFormData(formData))
.retrieve()
.bodyToMono(clazz)
.block();
}

关于java - 使用 Webflux 实现 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053929/

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