content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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.alipay.common.utils;
  
  import org.jetbrains.annotations.NotNull;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.nio.charset.StandardCharsets;
  import java.util.Map;
  
  public class HttpUtils {
  
      public static String sendPostRequest(String url, Map<String, String> header, String paramJson) {
          try {
              HttpURLConnection con = getHttpURLConnection(url, header);
              OutputStream os = con.getOutputStream();
              os.write(paramJson.getBytes(StandardCharsets.UTF_8));
              os.flush();
              os.close();
              BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
              String inputLine;
              StringBuilder response = new StringBuilder();
              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
              return response.toString();
          } catch (Exception e) {
              return null;
          }
      }
  
      @NotNull
      private static HttpURLConnection getHttpURLConnection(String url, Map<String, String> header) throws IOException {
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
          con.setRequestMethod("POST");
          con.setRequestProperty("Accept", "application/json");
          con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  
          if (header != null) {
              for (Map.Entry<String, String> entry : header.entrySet()) {
                  con.setRequestProperty(entry.getKey(), entry.getValue());
              }
          }
          con.setDoOutput(true);
          return con;
      }
  }
  