title: Convert PHP curl request to Java
body: <p>So I have this PHP code which makes a POST request to the specified URL. The $data array acts as a filter to apply to the request (for example to display the second page and maximum 10 items for that page). By default that values are "currentPage => 1" and "itemsPerPage => 100" (I'm accessing an API).</p>
  
  <pre><code>&lt;?php
  
  $usercode = '';
  $username = '';
  $password = '';
  $URL = '';
  
  $data =
      array (
          'currentPage' =&gt; 2,
          'itemsPerPage' =&gt; 10
      );
  
  $hash = sha1(http_build_query($data) . sha1($password));
  
  $requestData = array(
      'code' =&gt; $usercode,
      'username' =&gt; $username,
      'data' =&gt; $data,
      'hash' =&gt; $hash);
  
  $ch = curl_init();
  
  curl_setopt($ch, CURLOPT_URL, $URL);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
  
  $result = curl_exec($ch);
  
  $json_pretty = json_encode(json_decode($result), JSON_PRETTY_PRINT);
  echo $json_pretty;
  </code></pre>
  
  <p>It works exactly  as intended using PHP language, but I want to achieve the same behaviour in Java. The problem is that the filters doesn't apply (for every request the values of filters are ignored and the default values are used). I tried to make the request in the same form as in PHP, but for some reason the filters doesn't apply. This is my attempt using Apache's HttpClient:</p>
  
  <pre><code>public class Foo
  {
      private static String httpBuildQuery(List&lt;? extends NameValuePair&gt; parameters) {
          return URLEncodedUtils.format(parameters, "UTF-8").replace("*", "%2A");
      }
  
      public static void main(String[] args)
      {
          String username = "";
          String password = "";
          String usercode = "";
          final String URL = "";
  
          String headerValueToBeEncoded = username + ":" + password;
          String encodedHeaderValue = Base64.getEncoder().encodeToString(headerValueToBeEncoded.getBytes());
  
          List&lt;NameValuePair&gt; data = new ArrayList&lt;&gt;();
          data.add(new BasicNameValuePair("currentPage", "2"));
          data.add(new BasicNameValuePair("itemsPerPage", "10"));
  
          String passwordHash = DigestUtils.sha1Hex(password);
          String dataQueryString = Foo.httpBuildQuery(data);
          String valueToBeHashed = dataQueryString + passwordHash;
          String hash = DigestUtils.sha1Hex(valueToBeHashed);
  
          List&lt;NameValuePair&gt; requestData = new ArrayList&lt;&gt;();
          requestData.add(new BasicNameValuePair("code", usercode));
          requestData.add(new BasicNameValuePair("username", username));
          requestData.add(new BasicNameValuePair("data", data.toString()));
          requestData.add(new BasicNameValuePair("hash", hash));
  
          String requestDataQueryString = Foo.httpBuildQuery(requestData);
  
          try (CloseableHttpClient client = HttpClientBuilder.create().build())
          {
              HttpPost request = new HttpPost(URL);
              request.setHeader("Authorization", "Basic " + encodedHeaderValue);
              request.setEntity(new StringEntity(requestDataQueryString));
  
              HttpResponse response = client.execute(request);
  
              BufferedReader bufReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  
              StringBuilder builder = new StringBuilder();
              String line;
  
              while ((line = bufReader.readLine()) != null)
              {
                  builder.append(line);
                  builder.append(System.lineSeparator());
              }
  
              JSONObject jsonObject = new JSONObject(builder.toString());
              JSONArray jsonArray = jsonObject.getJSONArray("results");
  
              for(int i=0; i &lt; jsonArray.length(); i++)
              {
                  System.out.println(jsonArray.getJSONObject(i).get("id"));
              }
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The values of predefined that predefined strings (username, password, etc) have been left empty here because they contain sensitive information. In the last part I have printed only the values of the id's to check if the filters have been applied, but for every request the it prints 100 id's.</p>