content: /**
   * Copyright (C) 2024 AIDC-AI
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alibaba.langengine.tool.utils;
  
  import com.alibaba.fastjson.JSON;
  
  import lombok.extern.slf4j.Slf4j;
  import org.apache.http.client.methods.CloseableHttpResponse;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.HttpClients;
  import org.apache.http.util.EntityUtils;
  
  import java.io.IOException;
  import java.util.Map;
  
  @Slf4j
  public class PostUtils {
  
      public static String doPostWithJson(String url, Map<String, Object> param) {
          CloseableHttpClient httpClient = HttpClients.createDefault();
          CloseableHttpResponse response = null;
          String resultString = "";
          try {
              HttpPost httpPost = new HttpPost(url);
              httpPost.setHeader("Content-type", "application/json");
              if (param != null) {
                  StringEntity entity = new StringEntity(JSON.toJSONString(param), "application/json", "UTF-8");
                  // 模拟表单
                  httpPost.setEntity(entity);
                  response = httpClient.execute(httpPost);
                  resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
                  response.close();
              }
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  if (response != null) {
                      response.close();
                  }
                  httpClient.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return resultString;
      }
  }
  