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

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个回答

HttpClient 会自动管理它的cookie。 如果登录成功的话,直接该对象访问数据 可以直接访问的。。 检查下 是否登录请求成功。

qq_37471629
qq_37471629 是这样吗???
接近 3 年之前 回复
qq_37471629
qq_37471629 回复u010500435: NameValuePair[] postData = { new NameValuePair("StuNum", userName), new NameValuePair("Password", password), new NameValuePair("__EVENTARGUMENT", ""), new NameValuePair("__EVENTTARGET", ""),//略后。。。。 }; postMethod.setRequestBody(postData);
接近 3 年之前 回复
u010500435
u010500435 除了用户名和密码外,还有其他的隐藏参数。StuNum,ddlCollege,Password,__EVENTVALIDATION,__VIEWSTATE 本身__VIEWSTATE,__EVENTVALIDATION为页面hidden的文本框
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问