org.apache.hc.client5.http.entity.UrlEncodedFormEntity Java Examples

The following examples show how to use org.apache.hc.client5.http.entity.UrlEncodedFormEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Hc5HttpUtils.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HTTP POST Method.
 * 
 * @param url URL of the request.
 * @param properties Properties to drive the API.
 * @return POST Method
 * @throws URISyntaxException Exception if the URL is not correct.
 */
private static HttpPost createHttpPostMethod(
    String url,
    Map<String, String> properties) throws URISyntaxException {

  // Initialize POST method
  StringBuilder debugUrl = (DEBUG_URL) ? new StringBuilder("POST " + url) : null;
  HttpPost method = new HttpPost(new URIBuilder(url, StandardCharsets.UTF_8).build());
  method.addHeader("Accept-Encoding", "gzip");

  // Manage query parameters
  if (properties != null) {
    boolean first = true;
    List<org.apache.hc.core5.http.NameValuePair> params = new ArrayList<>();
    Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> property = iter.next();
      String key = property.getKey();
      String value = property.getValue();
      params.add(new BasicNameValuePair(key, value));
      first = fillDebugUrl(debugUrl, first, key, value);
    }
    method.setEntity(new UrlEncodedFormEntity(params));
  }

  if (DEBUG_URL && (debugUrl != null)) {
    debugText(debugUrl.toString());
  }
  return method;
}