很奇怪!Java web问题,模拟登陆教务系统刷新两次后就没反应了!

发布于 09-02 01:21 字数 4036 浏览 20 评论 0

我用HttpClient 4.5模拟登陆自己学校的教务系统,我们学校登陆成功先返回一个302跳转(勿喷,正方教务。。。):
先获取隐藏值:

/**

 * 获取隐藏值
 * @throws ClientProtocolException
 * @throws IOException
 */
private void getHiddenValues() throws ClientProtocolException, IOException {
    HttpGet get = new HttpGet(loginURL);
    RequestConfig config = RequestConfig.custom().
            setSocketTimeout(connectTimeout).
            setConnectTimeout(socketTimeout).build();
    get.setHeader("User-Agent", userAgent);
    get.setConfig(config);
    CloseableHttpResponse response = client.execute(get);
    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = response.getEntity();
        String rawHtml = EntityUtils.toString(entity);
        Document doc = Jsoup.parse(rawHtml);
        Element viewStateInput = doc.select("input[name=__VIEWSTATE]").first();
        Element generatorInput = doc.select("input[name=__VIEWSTATEGENERATOR]").first();
        Element validationInput = doc.select("input[name=__EVENTVALIDATION]").first();
        // 网络有问题
        if(viewStateInput == null) return;
        params.add(new BasicNameValuePair("__VIEWSTATE", viewStateInput.attr("value")));
        if(generatorInput != null)
            params.add(new BasicNameValuePair("__VIEWSTATEGENERATOR", 
                    generatorInput.attr("value")));  
        if(validationInput != null)
            params.add(new BasicNameValuePair("__EVENTVALIDATION", 
                    validationInput.attr("value")));
    }
}

然后模拟登陆(其余的参数都是用firebug看出来的)

/**
     * 登录
     * 
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public boolean login(String account, String password) throws IOException {
        getHiddenValues();
        if(params.size() > 0) {
            HttpPost post = new HttpPost(redirectURL);
            RequestConfig config = RequestConfig.custom()
                    .setSocketTimeout(connectTimeout)
                    .setConnectTimeout(socketTimeout)
                    .setRedirectsEnabled(false)
                    .build();
            post.setConfig(config);
            post.setHeader("User-Agent", userAgent);
            // 账号
            params.add(new BasicNameValuePair("TextBox1", account));
            // 密码
            params.add(new BasicNameValuePair("TextBox2", password));
            // 学生
            params.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));
            params.add(new BasicNameValuePair("Button1", ""));
            params.add(new BasicNameValuePair("lbLanguage", ""));
            post.setEntity(new UrlEncodedFormEntity(params, "GBK"));
            CloseableHttpResponse response = client.execute(post);
             // 获取响应状态码
            int status = response.getStatusLine().getStatusCode();
            // 302表示重定向状态
            if(status == 302) {
                return true;
            } else
                return false;
        }
        return false;
    }

第一次执行登陆成功了:
图片描述

第二次失败了。。。(看了看是500)
图片描述

第三次无反应了(一直没返回,调试看了看,在第一次方法的response那里阻塞了,我都设置了超时了。。。)。。。
图片描述

我是用SpringMVC框架做的,
这是控制器代码:

@Controller

    public class IndexController {
        
        @RequestMapping({"", "/"})
        public ModelAndView index() {
            ModelAndView mdv = new ModelAndView("index");
            HttpUtil util = new HttpUtil();
            try {
                mdv.addObject("result", util.login("*******", "*********"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return mdv;
        }
    }

代码封装的很烂,勿喷!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢2022-09-09 01:21:58

CloseableHttpResponse这个用完是要关闭的

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    <...>
} finally {
    response.close();
}
飘落散花2022-09-09 01:21:58

这是之后几次错误的显示页面:
图片描述

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文