title: Login on HTTPS website with java
body: <p>I'm trying to build a Java program, that automatically logs in into a website using https protocol. I just want to input my name and password into my prog, hit submit and get the page that would appear, if i did that in browser. </p>
  
  <p>I already wrote a program that shows the html code of the site using SSLSocket class.</p>
  
  <pre><code>import java.io.PrintWriter;
  import java.io.UnsupportedEncodingException;
  import java.util.Scanner;
  
  import javax.net.ssl.SSLSocket;
  import javax.net.ssl.SSLSocketFactory;
  
  public class bankpost{
      public static void main(String[] args) throws UnsupportedEncodingException {
      final String page = "banking.postbank.de";
      SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
      try {
          SSLSocket s = (SSLSocket) sf.createSocket(page, 443);
  
          Scanner in = new Scanner(s.getInputStream());
          PrintWriter out = new PrintWriter(s.getOutputStream(), true);
  
          System.out.println("Sending request to server "
                  + s.getInetAddress());
          out.print("POST /rai/login HTTP1.1\r\n\r\n");
          String content = "nutzername=a&amp;kennwort=a";
          String cmd = "POST /rai/login HTTP/1.1\n"
                  + "Host: banking.postbank.de\n"
                  + "Content-Type: application/x-www-form-urlencoded\n"
                  + "Content-Length: " + content.length() + "\n\n"
                  + content + "\r\n\r\n";
          out.print(cmd);
          out.flush();
          in.useDelimiter("&gt;");
          System.out.println("Reading...");
          int len = 0;
          while (in.hasNext()) {
              String line = in.next();
              len += line.length();
              if (line.contains("field-input") || line.contains("nicht"))
                  System.err.println(line.trim());
              else
                  System.out.println(line.trim());
          }
  
          System.out.println("DONE - " + len);
          System.out.print(cmd);
          in.close();
          out.close();
          s.close();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>...the fields I want to put input to are "nutzername" and "kennwort".</p>
  
  <p>What I am doing wrong here?</p>