首页 > 解决方案 > 是否可以在 Java 或 JavaScript 中发出 HTTP (POST) 请求而无需等待响应

问题描述

我需要创建一个 http post 请求,而无需等待 Java 或 JavaScript 的响应。异步 http 调用不是我想要的,因为它仍在等待响应,只是在一个单独的线程上。

标签: javascriptjavajqueryspringasynchronous

解决方案


是的,有可能,
我之前在使用 laravel 时已经完成了,我需要大量的 webhook 发送给商家。在我写登录等待响应之前,但它需要更多时间然后我谷歌并找到解决方案。这是示例。

function postCurlRequest(string $url, array $post_array, $check_ssl=true) {
    // $url = 'https://post_url.com';
    $cmd = "curl -L -X POST -H 'Content-Type: application/json'";
    $cmd.= " -d '" . json_encode($post_array) . "' '" . $url . "'";

    if (!$check_ssl){
      $cmd.= "'  --insecure"; // this can speed things up, though it's not secure
    }
    $cmd .= " > /dev/null 2>&1 &"; // don't wait for response

    // echo $cmd;die;

    exec($cmd, $output, $exit);
   return $exit == 0;
}

推荐阅读