package loginApp;
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;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class ImitateLogin {
public static void main(String[] args) {
String userName = "1307012027"; // 本人学号
String password = "360731*********"; // 本人密码
String loginUrl = "http://jwc.jxnu.edu.cn/Default_Login.aspx?preurl=";
String dataUrl = "http://jwc.jxnu.edu.cn";
HttpClientLogin(userName, password, loginUrl, dataUrl);
}
private static void HttpClientLogin(String userName, String password,
String loginUrl, String dataUrl) {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
PostMethod postMethod = new PostMethod(loginUrl);
NameValuePair[] postData = { new NameValuePair("StuNum", userName),
new NameValuePair("Password", password) };
postMethod.setRequestBody(postData);
try {
httpClient.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(postMethod);
Cookie[] cookies = httpClient.getState().getCookies();
StringBuffer stringBuffer = new StringBuffer();
for (Cookie c : cookies) {
stringBuffer.append(c.toString() + ";");
}
GetMethod getMethod = new GetMethod(dataUrl);
getMethod.setRequestHeader("Cookie", stringBuffer.toString());
postMethod.setRequestHeader("Host", "http://jwc.jxnu.edu.cn");
postMethod.setRequestHeader("Referer", "http://jwc.jxnu.edu.cn/Default_Login.aspx?preurl=");
postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
httpClient.executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}