qq_37471629
qq_37471629
2017-04-18 06:28

模拟登录教务在线,为什么进去的不是登录后的界面,而是登录前的界面,求大神帮忙,毕业论文需要

  • httpclient模拟登录

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();
    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答