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

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

我用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;
        }
    }

代码封装的很烂,勿喷!


这是之后几次错误的显示页面:


CloseableHttpResponse这个用完是要关闭的

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    <...>
} finally {
    response.close();
}
【热门文章】
【热门文章】