package com.main;
import java.io.InputStreamReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Login {
public static void main(String[] args) {
String loginUrl = "https://www.oschina.net/action/user/hash_login";
String email = "email";
String password = "password";
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(new InputStreamReader(Login.class
.getResourceAsStream("/sha1.js")));
Object t = engine.eval("CryptoJS.SHA1('" + password
+ "').toString();");
System.out.println(t);
HttpClient client = new HttpClient();
NameValuePair[] nameValuePairs = {
new NameValuePair("email", email),
new NameValuePair("pwd", t.toString()) };
PostMethod postMethod = new PostMethod(loginUrl);
postMethod.setRequestBody(nameValuePairs);
client.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
client.executeMethod(postMethod);
Cookie[] cookies = client.getState().getCookies();
String tmpcookies = "";
for (Cookie c : cookies) {
tmpcookies += c.toString() + ";";
}
GetMethod getMethod = new GetMethod(
"http://my.oschina.net/snageyang/admin/inbox");
getMethod.setRequestHeader("cookie", tmpcookies);
client.executeMethod(getMethod);
String text = getMethod.getResponseBodyAsString();
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}