title: Java - sending HTTP parameters via POST method easily
body: <p>I am successfully using this code to send  <code>HTTP</code> requests with some parameters via <code>GET</code> method</p>
  
  <pre><code>void sendRequest(String request)
  {
      // i.e.: request = "http://example.com/index.php?param1=a&amp;param2=b&amp;param3=c";
      URL url = new URL(request); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
      connection.setDoOutput(true); 
      connection.setInstanceFollowRedirects(false); 
      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("Content-Type", "text/plain"); 
      connection.setRequestProperty("charset", "utf-8");
      connection.connect();
  }
  </code></pre>
  
  <p>Now I may need to send the parameters (i.e. param1, param2, param3) via <code>POST</code> method because they are very long.
  I was thinking to add an extra parameter to that method (i.e. String httpMethod).</p>
  
  <p>How can I change the code above as little as possible to be able to send paramters either via <code>GET</code> or <code>POST</code>?</p>
  
  <p>I was hoping that changing</p>
  
  <pre><code>connection.setRequestMethod("GET");
  </code></pre>
  
  <p>to</p>
  
  <pre><code>connection.setRequestMethod("POST");
  </code></pre>
  
  <p>would have done the trick, but the parameters are still sent via GET method.</p>
  
  <p>Has <code>HttpURLConnection</code> got any method that would help?
  Is there any helpful Java construct?</p>
  
  <p>Any help would be very much appreciated.</p>