title: Java send post request for login
body: <p>i want to login to application from java code.</p>
  
  <p>I have found the following code example:</p>
  
  <pre><code> String requestURL = "http://applicationURL/login.jsp"; 
   String data = "email=temail@mail.com&amp;password=123password&amp;login=Login";
  
  
   public static String sendPostRequest(String data, String requestURL) {
  
           String result="";
          try {
  
              // Send the request
              URL url = new URL(requestURL);
              URLConnection conn = url.openConnection();
              conn.setDoOutput(true);
              OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  
              //write parameters
              writer.write(data);
              writer.flush();
  
              // Get the response
              StringBuffer answer = new StringBuffer();
              BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              String line;
              while ((line = reader.readLine()) != null) {
                  answer.append(line);
              }
              writer.close();
              reader.close();
  
              //Output the response
              System.out.println(answer.toString());
              result = answer.toString();
  
          } catch (MalformedURLException ex) {
              ex.printStackTrace();
          } catch (IOException ex) {
              ex.printStackTrace();
          }
           return result;
      }
  </code></pre>
  
  <p>but i can not login, it returns back only login page.</p>
  
  <p>If anybody can, please help me to understand what am i doing wrong.</p>
  
  <p>I have added method and content-type, but it still does not work:</p>
  
  <pre><code>conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  </code></pre>