title: I am getting 411 HTTP Error while using POST method in Java
body: <p>I am facing a problem while using <code>POST</code> Method in Java nowadays. I am receiving</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; java.lang.RuntimeException: Server returned HTTP response code: <code>411</code> for URL.</p>
  </blockquote>
  <p>I couldn't find any available document anywhere. None of them were useful. How do I fix it?</p>
  <h3>My code</h3>
  <pre><code>import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  public class req {
      public static void main(String[] args) {
          sendPostRequest(requestURL);
      }
  
      private static String sendPostRequest(String requestUrl) {
          StringBuilder jsonString = new StringBuilder();
          try {
              URL url = new URL(requestUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              byte[] data = requestUrl.getBytes(&quot;UTF-8&quot;);
  
  
              connection.setDoInput(true);
              connection.setDoOutput(true);
              connection.setRequestMethod(&quot;POST&quot;);
              connection.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;);
              connection.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json; charset=UTF-8&quot;);
              connection.setRequestProperty(&quot;Content-Length&quot;, String.valueOf(data.length));
              connection.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + &quot;usename:password&quot;);
  
              BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              String line;
              while ((line = br.readLine()) != null) {
                  jsonString.append(line);
              }
              br.close();
              connection.disconnect();
          } catch (Exception e) {
              throw new RuntimeException(e.getMessage());
          }
  
          return jsonString.toString();
      }
  }
  </code></pre>