利用httpclient进行模拟登陆;

发现过程中存在302重定向的情况,解决方法,获得地址,再次发送请求。httpclient是同个,二次发送请求,会保存session id ,即可进行访问。

public class Main {

    static String charset = "utf-8";
    static String result = "";
    private static CloseableHttpClient httpClient = HttpClients.createDefault();
    private static HttpClientContext context = new HttpClientContext();

    public static void main(String[] args) {

        String url = "http://hdu.sunnysport.org.cn/login";
        String param = "username=14031601&password=14031601";
        // System.out.println(sendURLPost(url, param));
        CloseableHttpResponse response = null;
        String content = null;
        BasicCookieStore cookieStore = new BasicCookieStore();
        /**
         * 使用httpclient
         */
        List nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("username", "14031601"));
        nvps.add(new BasicNameValuePair("password", "14031601"));

        try {
            // HttpClient中的post请求包装类
            HttpPost post = new HttpPost(url);
            // nvps是包装请求参数的list
            if (nvps != null) {
                post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            }
            // 执行请求用execute方法,content用来帮我们附带上额外信息
            response = httpClient.execute(post, context);
            // 看是否重定向,并测试
            System.out.println(response.getStatusLine().getStatusCode());
            Header firstHeader = response.getFirstHeader("Location");
            System.out.println("name:" + firstHeader.getName());
            System.out.println("value:" + firstHeader.getValue());
            if (firstHeader.getValue() != null) {

                System.out.println("开始重定向=======");
                HttpPost post2 = new HttpPost(firstHeader.getValue());
                // nvps是包装请求参数的list
                if (nvps != null) {
                    post2.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
                }
                // 执行请求用execute方法,content用来帮我们附带上额外信息
                response = httpClient.execute(post2, context);

                System.out.println(response.getStatusLine().getStatusCode());
                Header firstHeader2 = response.getFirstHeader("Location");

            }
            // 得到相应实体、包括响应头以及相应内容
            HttpEntity entity = response.getEntity();

            content = EntityUtils.toString(entity);
            // 关闭输入流
            EntityUtils.consume(entity);
            /**
             * 重定向并不需要使用cookies,此处多余
             */
            List cookies = cookieStore.getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(content);
            }
        }
    }

}

你可能感兴趣的:(java学习)