title: Login to PHP based website via Java Application
body: <p>i searched this site for a solution to my problem but couldn't find a solution, please help</p>
  
  <p>I'm trying to login to my company website with java which is designed using php.</p>
  
  <p>There are many tutorials out there and i managed to scrounge some code and put it together but it doesn't seem to be working</p>
  
  <pre><code>package testphplogin;
  
  import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  
  public class HttpURLConnectionExample {
  
  private final String USER_AGENT = "Mozilla/5.0";
  
  public static void main(String[] args) throws Exception {
  
      HttpURLConnectionExample http = new HttpURLConnectionExample();
  
      System.out.println("Testing 1 - Send Http GET request");
      http.sendGet();
  
      System.out.println("\nTesting 2 - Send Http POST request");
      http.sendPost();
  
  }
  
  // HTTP GET request
  private void sendGet() throws Exception {
  
      String url = "http://www.omnitech.co.nz/repair/login.php?";
  
      URL obj = new URL(url);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
      // optional default is GET
      con.setRequestMethod("GET");
  
      //add request header
      con.setRequestProperty("User-Agent", USER_AGENT);
  
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'GET' request to URL : " + url);
      System.out.println("Response Code : " + responseCode);
  
      StringBuilder response;
      try (BufferedReader in = new BufferedReader(
              new InputStreamReader(con.getInputStream()))) {
          String inputLine;
          response = new StringBuilder();
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
      }
  
      //print result
      System.out.println(response.toString());
  
  }
  
  // HTTP POST request
  private void sendPost() throws Exception {
  
      System.setProperty("jsse.enableSNIExtension", "false");
  
      String url = "http://www.omnitech.co.nz/repair/login.php";
  
      URL obj = new URL(url);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
      //add reuqest header
      con.setRequestMethod("POST");
      con.setRequestProperty("User-Agent", USER_AGENT);
      con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  
      String urlParameters = "cLogin=&lt;CENSORED&gt;&amp;cPsw=&lt;CENSORED&gt;&amp;type=tbTech&amp;Submit=Login";
  
      // Send post request
      con.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
      wr.writeBytes(urlParameters);
      wr.flush();
      wr.close();
  
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + urlParameters);
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(
              new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuilder response = new StringBuilder();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      System.out.println(response.toString());
  
  }
  
  }
  </code></pre>
  
  <p>Here is the result</p>
  
  <pre><code>run:
  Testing 1 - Send Http GET request
  
  Sending 'GET' request to URL : http://www.omnitech.co.nz/repair/login.php?
  Response Code : 200
  &lt;TOO LONG CENSORED&gt;
  
  Testing 2 - Send Http POST request
  &lt;TOO LONG CENSORED&gt;
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>
  
  <p>It just returns me to the login page</p>
  
  <p>This is the HTML form that gets POSTed</p>
  
  <pre><code>&lt;form name="form1" method="POST" action="/repair/login.php"&gt;  
      &lt;input name="cLogin" type="text" id="cLogin" placeholder="Login"&gt;
      &lt;input name="cPsw" type="password" id="cPsw" placeholder="Password"&gt;
      &lt;div class="six columns"&gt;
          &lt;input name="type" type="radio" id="tbtech" value="tbtech" checked=""&gt;
          &lt;label for="tbtech"&gt;
              Technician
          &lt;/label&gt;
      &lt;/div&gt;
  
      &lt;div class="six columns"&gt;
          &lt;input type="radio" id="tbopr" name="type" value="tbopr"&gt;
          &lt;label for="tbopr"&gt;Operator&lt;/label&gt;
      &lt;/div&gt;
  </code></pre>