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

    使用HttpClient来模拟浏览器登陆网站,而后能够进行操做,好比发布信息等浏览器

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

  一、须要使用到firefox的httpfox插件,httpfox中clear一下,而后start开始捕获网站

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

  三、这样就获得了模拟登陆时须要Post的数据参数(Parameter)值(Value),以及实际Post的网址URLspa

    第二步,使用HttpClient来登陆firefox

  一、简单核心代码以下插件

  

 1         CloseableHttpClient httpclient = HttpClients.createDefault();
 2         List<NameValuePair> postData = new ArrayList<NameValuePair>();
 3         //这里可能有多个参数
 4         postData.add(new BasicNameValuePair("username", "username"));
 5         postData.add(new BasicNameValuePair("password", "password"));
 6         //URL是实际的post地址,使用httpFox获得
 7         HttpPost httppost = new HttpPost(URL); 9         try {11                 httppost.setEntity(new UrlEncodedFormEntity(postData, "GBK"));
12                 response = httpclient.execute(httppost);
15         } catch (IOException e) {
16         } finally {
17             closeIO(response);
18         }
相关文章
相关标签/搜索