title: Use CORS in a http request
body: <p>Today, i have the following problem. I want to make an http request to an rest api, but I only get an 405 error. I searched and i found, that the api use cors.
  So how i can make a http request with cors?</p>
  
  <p>Here is my http client:</p>
  
  <pre><code>package community.opencode.minetools4j.util.http;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.NonNull;
  import org.apache.commons.io.IOUtils;
  
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.net.URLEncoder;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * This is an utility class.
   * It simplify making HTTP requests
   */
  public class HttpRequest {
  
      /**
       * Performs a new HTTP request
       *
       * @param requestBuilder See {@link RequestBuilder}
       * @return See {@link RequestResponse}
       * @throws IOException Will be thrown if the request can't be executed
       */
      public static RequestResponse performRequest(RequestBuilder requestBuilder) throws IOException {
          URL newUrl = new URL(requestBuilder.getPath());
  
          HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection();
          connection.setRequestMethod(requestBuilder.getMethod().getType());
          requestBuilder.getHeaders().forEach(connection::setRequestProperty);
          connection.setConnectTimeout(5000);
          connection.setReadTimeout(5000);
          connection.setDoOutput(true);
  
          DataOutputStream out = new DataOutputStream(connection.getOutputStream());
          out.writeBytes(getParametersString(requestBuilder.getParams()));
  
          int status = connection.getResponseCode();
          String result = IOUtils.toString(connection.getInputStream(), "UTF-8");
          String contentType = connection.getHeaderField("Content-Type");
          connection.getInputStream().close();
          connection.disconnect();
          return new RequestResponse(status, result, contentType);
      }
  
      /**
       * Makes from a map a valid HTTP string
       * Example: "myField=Hey!&amp;myPassword=abc"
       *
       * @param params The parameters
       * @return The formed String
       * @throws UnsupportedEncodingException Should never be thrown
       */
      private static String getParametersString(Map&lt;String, String&gt; params) throws UnsupportedEncodingException {
          StringBuilder builder = new StringBuilder();
          for (Map.Entry&lt;String, String&gt; entry : params.entrySet()) {
              builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
              builder.append("=");
              builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
              builder.append("&amp;");
          }
          String result = builder.toString();
          return result.length() &gt; 0 ? result.substring(0, result.length() - 1) : result;
      }
  
      /**
       * A simple builder class for {@link #performRequest(RequestBuilder)}
       */
      @Data
      public static class RequestBuilder {
  
          private final String path;
          private final HttpRequestMethod method;
  
          private final Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
          private final Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
  
          public RequestBuilder addParam(@NonNull String name, @NonNull String value) {
              this.params.put(name, value);
              return this;
          }
  
          public RequestBuilder addHeader(@NonNull String name, @NonNull String value) {
              this.headers.put(name, value);
              return this;
          }
      }
  
      /**
       * A simplified http response.
       * Including status, message and the returned content type
       */
      @Data
      @AllArgsConstructor
      public static class RequestResponse {
  
          private int status;
          private String resultMessage;
          private String contentType;
      }
  }
  
  package community.opencode.minetools4j.util.http;
  
  import lombok.Getter;
  
  /**
   * Simple enum for {@link HttpRequest}.
   * It's a better way than just write manually the method name
   */
  public enum HttpRequestMethod {
      POST("POST"),
      GET("GET"),
      PUT("PUT"),
      HEAD("HEAD"),
      OPTIONS("OPTIONS"),
      DELETE("DELETE"),
      TRACE("TRACE");
  
      @Getter
      private String type;
  
      HttpRequestMethod(String type) {
          this.type = type;
      }
  }
  </code></pre>
  
  <p>And here is my method to make a http request:</p>
  
  <pre><code>HttpRequest.RequestResponse requestResponse = HttpRequest.performRequest(new HttpRequest.RequestBuilder(API_URI + "/ping/" + host + "/" + port, HttpRequestMethod.GET));
  </code></pre>
  
  <p>Thank you for your attention.</p>
  
  <p>Sincerely,
  Skyleiger</p>
  
  <p>Sorry for my bad english. I hope you can understand me ;)</p>