首頁  >  問答  >  主體

帶有識別符和秘密或散列密碼的 SpringBoot WebClient 請求

我有一個 curl php 請求,如下所示

$curlInit = curl_init();
curl_setopt($curlInit, CURLOPT_URL, 'https://www.myurl.com/');
curl_setopt($curlInit, CURLOPT_POST, 1);
curl_setopt($curlInit, CURLOPT_POSTFIELDS,
    http_build_query(
        array(
            'action' => 'GetSearchDetails',
            'username' => 'lambistic',
            'password' => 'lambistic######',
            'responsetype' => 'json',
        )
    )
);
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlInit);
curl_close($curlInit);

我想要實現的是在 springboot java 中使用 web client 執行相同的操作,以下是我嘗試使用 Web 用戶端執行此操作的方式

public Mono<SearchDetailsResponse> sendSearchDetailsRequest() {

        return webClient.post()
                .uri("https://www.myurl.com/")
                .header("Content-Type", "application/json")
                .headers(httpHeaders -> {
                    httpHeaders.set("username", "lambistic");
                    httpHeaders.set("password", "lambistic######");
                })
                .retrieve()
                .bodyToMono(SearchDetailsResponse.class);
}

我沒有得到任何回复,我的網路客戶端可能做錯了什麼

P粉825079798P粉825079798219 天前384

全部回覆(1)我來回復

  • P粉510127741

    P粉5101277412024-02-18 15:02:36

    http_build_query 新增參數作為查詢字串。 在您的網路用戶端中,您將它們新增為標頭。我想你必須改變這一點:

    webClient
    .post()
    .uri("https://www.myurl.com?action=GetSearchDetails&username=lambistic&password=lambistic#####&responsetype=json")
    .retrieve()
    .bodyToMono(SearchDetailsResponse.class);

    回覆
    0
  • 取消回覆