AUTOLOGIN To Moodle

185 views
Skip to first unread message

pratik lodha

unread,
Mar 19, 2014, 2:52:55 PM3/19/14
to wncc...@googlegroups.com
A java program,but with errors ,need help 



import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class tp {
private List<String> cookies;
private URLConnection conn;
private final String USER_AGENT="Mozilla/5.0";
public static void main(String[] args)throws Exception {
String moodle="http://moodle.iitb.ac.in";
tp http=new tp();
CookieHandler.setDefault(new CookieManager());
String page =http.GetPageContent(moodle);
String postParams = http.getFormParams(page, "130040012", "password");
http.sendPost(url,postParams);
String result = http.GetPageContent(moodle);
System.out.println(result);
}
private void sendPost(String url, String postParams)throws Exception {
URL obj = new URL(url);
conn = (URLConnection) obj.openConnection();
 
// Acts like a browser
conn.setUseCaches(false);
((HttpURLConnection) conn).setRequestMethod("POST");
conn.setRequestProperty("Host", "moodle.iitb.ac.in");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie",cookie.split(";",1)[0]);
}
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Referer", "http://moodle.iitb.ac.in/");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
 
conn.setDoOutput(true);
conn.setDoInput(true);
 
// Send post request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
 
int responseCode = ((HttpURLConnection) conn).getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
 
BufferedReader in = 
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// System.out.println(response.toString());
 
 }
 
private String getFormParams(String html, String username, String password)throws UnsupportedEncodingException {
System.out.println("Extracting form's data...");
 
Document doc = Jsoup.parse(html);
 
// Google form id
Element loginform = doc.getElementById("login");
Elements inputElements = loginform.getElementsByTag("input");
List<String> paramList = new ArrayList<String>();
for (Element inputElement : inputElements) {
String key = inputElement.attr("name");
String value = inputElement.attr("value");
 
if (key.equals("username"))
value = username;
else if (key.equals("Password"))
value = password;
paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
}
 
// build parameters list
StringBuilder result = new StringBuilder();
for (String param : paramList) {
if (result.length() == 0) {
result.append(param);
} else {
result.append("&" + param);
}
}
return result.toString();

}
private String GetPageContent(String url)throws Exception {
URL obj = new URL(url);
conn = (URLConnection) obj.openConnection();
 
// default is GET
((HttpURLConnection) conn).setRequestMethod("GET");
 
conn.setUseCaches(false);
 
// act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
if (cookies != null) {
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
}
int responseCode = ((HttpURLConnection) conn).getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
 
BufferedReader in = 
           new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
 
// Get the response cookies
setCookies(conn.getHeaderFields().get("Set-Cookie"));
 
return response.toString();
 
}
public List<String> getCookies() {
return cookies;
 }
 
 public void setCookies(List<String> cookies) {
this.cookies = cookies;
 }
}

Manish Goregaokar

unread,
Mar 19, 2014, 5:12:11 PM3/19/14
to wncc...@googlegroups.com
Please explain what the errors are.

Also, you might want to try it in Python. It's been done before: herehere , and here, and the login takes a very small amount of code (plus you can just copy it!)



-Manish Goregaokar


--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dilawar Singh

unread,
Mar 22, 2014, 6:21:14 AM3/22/14
to wncc...@googlegroups.com
Seems to be compiling fine on my system. Check where file `jsoup.jar` is located. Use this on command line.

javac -cp /usr/share/java/jsoup.jar tp.java

-
Dilawar
Reply all
Reply to author
Forward
0 new messages