I want to make a java program to auto login at http://www.eclass.teikal.gr/eclass2/

When I run this I take as result the same page! Where am I doing wrong ?

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&pass=my_password&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());

}

}
有帮助吗?

解决方案

Try to submit this request from some other tool, for example firefox plugin HTTP Resource test. Then you will find if the problem is in your request or in your code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top