利用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<NameValuePair> 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<Cookie> 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);
            }
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值