Java语言使用HttpClient模拟浏览器登录

使用HttpClient来模拟浏览器登录网站,然后可以进行操作,比如发布信息等

第一步:获取实际的post网址,(不考虑复杂情况下)

  1、需要使用到firefox的httpfox插件,httpfox中clear一下,然后start开始捕获

  2、切换回网页的登录页面,开始输入自己的账号密码登录,登录成功后切回httpfox中stop,查看最近的post方法中包含的Post Data数据,和此post方法的url网址,

  3、这样就得到了模拟登录时需要Post的数据参数(Parameter)和值(Value),以及实际Post的网址URL

第二步,使用HttpClient来登录

  1、简单核心代码如下

CloseableHttpClient httpclient = HttpClients.createDefault();
        List postData = new ArrayList();
        //这里可能有多个参数
        postData.add(new BasicNameValuePair(“username”, “username”));
        postData.add(new BasicNameValuePair(“password”, “password”));
        //URL是实际的post地址,使用httpFox得到
        HttpPost httppost = new HttpPost(URL); 9        try {11                httppost.setEntity(new UrlEncodedFormEntity(postData, “GBK”));
                response = httpclient.execute(httppost);
        } catch (IOException e) {
        } finally {
            closeIO(response);
        }

HttpClient4.3 关于https 中SSL证书请求问题 http://www.linuxidc.com/Linux/2016-04/130090.htm

HttpClient4 用法 由HttpClient3 升级到 HttpClient4 必看  http://www.linuxidc.com/Linux/2015-06/119100.htm

HttpClient 教程  http://www.linuxidc.com/Linux/2015-06/119099.htm

使用HttpClient实现文件的上传下载 http://www.linuxidc.com/Linux/2014-07/104303.htm

Android 实现 HttpClient 请求Https  http://www.linuxidc.com/Linux/2014-05/102306.htm

Android使用HttpClient下载图片 http://www.linuxidc.com/Linux/2014-05/101855.htm

HttpClient使用详解  http://www.linuxidc.com/Linux/2014-08/104945.htm