title: Login to a webpage from java
body: <p>I want to make a java program to auto login at <a href="http://www.eclass.teikal.gr/eclass2/" rel="nofollow">http://www.eclass.teikal.gr/eclass2/</a></p>
  
  <p>When I run this I take as result the same page! Where am I doing wrong ?</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  import javax.net.ssl.HttpsURLConnection;
  
  public class HttpURLConnectionExample {
  
  private final String USER_AGENT = "Mozilla/5.0";
  
  public static void main(String[] args) throws Exception {
  
      HttpURLConnectionExample http = new HttpURLConnectionExample();
  
      http.sendPost();
  
  }
  
  
  
  // HTTP POST request
  private void sendPost() throws Exception {
  
  //      String url = "https://selfsolve.apple.com/wcResults.do";
      String url = "http://www.eclass.teikal.gr/eclass2/index.php";
      URL obj = new URL(url);
      URL obj1 = new URL ("http://www.eclass.teikal.gr/eclass2/");
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      HttpURLConnection con1 = (HttpURLConnection) obj1.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 = "uname=my_username&amp;pass=my_password&amp;submit=";
  
  
      // 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;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      //print result
      System.out.println(response.toString());
  
  }
  
  }
  </code></pre>