title: Java HTTP POST request vs. CURL
body: <p>How to translate the following:</p>
  
  <pre><code>curl -X POST -d 'param1=value1&amp;param2=value2' http://url/get_file
  </code></pre>
  
  <p>in Java?</p>
  
  <p>I tried as follows, but this does not work and the request is malformed.</p>
  
  <pre><code>HttpClient httpClient = HttpClients.createDefault();
  HttpPost request = new HttpPost("http://url/get_file");
  
  try {
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(2);
      params.add(new BasicNameValuePair("param1", value1));
      params.add(new BasicNameValuePair("param2", value2));
      request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
  
      HttpResponse response = httpClient.execute(request);
      HttpEntity entity = response.getEntity();
  
      if (entity != null) {
          InputStream instream = entity.getContent();
          instream.close();
      }
  }
  catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>